4

Is there an existing robust Java library which implements a fairly substantial subset of Perl regular expression syntax?

Background:

I wish to implement a file renamer where renaming is done using Perl regular expressions.

The trick is that the project containing said renamer as a component is, currently, 100% in Java in Windows.

NOTES:

I am setting aside the obvious workaround of "install Strawberry Perl; write the whole renamer in Perl (or teach Perl to the developer doing Java coding), pass a list of files to rename to the Perl renamer script via a system call from Java" as too crafty and too obvious :)

Also, please don't offer comparisons of how Java's latest and greatest RegEx engine is already good enough to do most of what Perl RegEx does - I'm fairly aware of its functionality (and can google); thus I already know that said statement might even be true; it is, however, irrelevant to my interest in seeing real Perl RegEx syntax implemented as a Java library.

DVK
  • 126,886
  • 32
  • 213
  • 327
  • How much is "fairly substantial"? The rest of your question reads like that would be in the 95% and above ballpark. – innaM Dec 15 '09 at 20:27
  • 2
    At the very least, you won't be able to get `(?{ code })` support without a Perl interpreter. – Anon. Dec 15 '09 at 20:28
  • 2
    Can you access PCRE (perl compatible regex) through JNI (Java Native Interface)? Not much of a Java hacker, so I don't know what's involved. – daotoad Dec 15 '09 at 20:35
  • JNI to Perl... *shudder.* Worst of both worlds... – Kevin Panko Dec 15 '09 at 20:43
  • @Manni - since, as the questions makes obvious, there are fairly doable workarounds, it is more of a "searching for a theoretically cool thing"; and thus I don't have a firm threshold. The more coverage the better. – DVK Dec 15 '09 at 20:46
  • @Kevin - not being familiar with JNI at all, could you please explain why it is the worst? Thx – DVK Dec 15 '09 at 20:49
  • 1
    @DVK You would be writing C code that interfaces with both the JVM and the Perl engine. So you would need convert Java's String object into a char pointer, then convert that into a Perl SV structure, call the regexp function, get another SV back, then convert that back to a char pointer, and finally back into a String object. It could be done, but I do not think it would be worth the trouble. – Kevin Panko Dec 15 '09 at 20:58
  • Looks kind of like this... http://cpansearch.perl.org/src/PATL/Inline-Java-0.52/Java/JNI.xs Guess someone already did the dirty work for you. – Kevin Panko Dec 15 '09 at 21:12

3 Answers3

7

Have you tried JRegex? It boasts Perl 5.6 compatibility and doesn't have the worries of linking PCRE in directly.

pestilence669
  • 5,698
  • 1
  • 23
  • 35
5

For maximum Perl compatibility you would need to actually use Perl. You can do that using Inline::Java::Callback, which is distributed as part of the Inline::Java module.

See also: How can I call Perl from Java?

Community
  • 1
  • 1
Michael Carman
  • 30,628
  • 10
  • 74
  • 122
3

The library you are looking for is the Apache ORO library, it is specifically implemented to handle Perl5 regular expressions.

rsp
  • 23,135
  • 6
  • 55
  • 69
  • Just as a note, the stable version only supports Perl5.003 as per their docs. But a good option nonetheless! – DVK Dec 16 '09 at 16:02