From: Guy Gascoigne-P. <gg...@tr...> - 2002-02-06 09:48:06
|
I guess that I didn't explain my self very clearly, though I think that you did answer my question. I've written a java module that extends jython, I want that java module to use regular expressions in the same way that jython does. As you say that would involve using sre and I can't do that from a java module :-( Thanks - Guy -----Original Message----- From: bc...@wo... [mailto:bc...@wo...] Sent: Wednesday, February 06, 2002 12:29 AM To: 'jyt...@li...' Cc: Guy Gascoigne-Piggford Subject: Re: [Jython-users] Accessing Jython Regular Expressions from java [Guy Gascoigne-Piggford] >OK, so I was happily just using the oromatcher Perl5Compiler() routines to >handle python style regular expressions in my java code. Basically the >regular expressions get passed from the jython script through to a java >function of mine and I wanted to handle them in a python compatible way. It >turns out that I wasn't completely correct. > >I got code that actually worked pretty well until I started passing >expressions such as this to it: > >"(source filename|Name of).*\?(?i)" > >The gotcha is the (?i) bit at the end, this generates a mismatched >parenthesis error. I'm a little confused here. Are you reporting a bug in the way jython does regular expression or in way Perl5Compiler does them? >Now after a lot of delving I've discovered that jython actually uses a >completely different regular expression library - the one in the sre module. >My question is, since this is all python code, is there any way that I can >just call out to this from java? So you want to use sre from a pure java (no jython) application? If that is what you're asking, the answer is no, it can't be done. >It appears to me that by moving over to a >python implementation here I can't use the same stuff - so far I've found >one incompatibility between the two implementations, and I'm concerned that >I'll run into others. My confusion continues. I can compile the example re you gave with jython-2.1 without any errors: Jython 2.1 on java1.4.0-beta3 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> import re >>> re.compile("(source filename|Name of).*\?(?i)") org.python.modules.sre.PatternObject@dd5a3d >Am I stuck with pre-processing the regular expressions and handling the (?i) >construct myself - I can't believe that's all that there is to it. The old Perl5Compiler based "pre" module did some fixup of the pattern before passing the pattern to Perl5Compiler. Se RegexObject.fixPattern() for details. >Any other alternatives? > >As an aside, why the switch to sre? Faster? No. >Better Python compatibility? Yes. The "pre" module have some known bugs in it. For example the parens counting in fixPattern doesn't work in all cases. regards, finn |