tags:

views:

162

answers:

3

I have some code that I'm converting from Perl to Java. It makes pretty heavy use of regular expressions, including the s/// operator. I've been using Perl for a long time and am still getting used to the Java way of doing things. In particular, Strings seem more difficult to work with. Does anyone know of or have a Java function that fully implements s///? So that it could handle something like this, for example:

$newString =~ s/(\bi'?\b)/\U$1/g;

(Maybe not a great example, but you get the idea.) Thanks.

+1  A: 

Have a look at String's replaceAll(...) method. Note that Java does not support the \U (upper-case) feature.

Bart Kiers
Thanks. I've also found Matcher.replaceAll(). Are they powerful enough to handle my example of upcasing the match, though...?
reid
`Matcher.replaceAll()` is used by `String`'s `replaceAll()`. So that answers your other question: no, `\U` is not supported.
Bart Kiers
I missed the \U part of your reply. I guess I have to accept that replaceAll() will work for most but not all cases.
reid
To emphasize: not only `replaceAll(...)` does not support it, but the entire Java regex package doesn't.
Bart Kiers
Thanks again, Bart (and to everyone else, too; my first question here!).
reid
You're welcome reid.
Bart Kiers
+6  A: 

Nothing so tidy, but in Java you would use String.replaceAll() or use Pattern to do something like:

Pattern p = Pattern.compile("(\bi'?\b)");

Matcher m = p.matcher(stringToReplace);
m.replaceAll("$1");

Check the Pattern docs for Java's regex syntax--it doesn't overlap completely with Perl.


To get uppercasing, check out Matcher.appendReplacement:

StringBuffer sb = new StringBuffer();
while (m.find()) {
    String uppercaseGroup = m.group(1).toUpperCase();
    m.appendReplacement(sb, uppercaseGroup);
}
m.appendTail();

Not as close to Perl as the jakarta-oro library referenced above, but definitely some help built into the library.

Michael Brewer-Davis
+2  A: 

Given an instance of a String class you can use the .replaceAll() method like so:

String A = "Test";
A.replaceAll("(\bi'?\b)","\U$1");

Edit - ok too slow. Also apparently \U isn't supported according to another answer.

Note - I'm not sure how the greedy symbol translates, you might want to try looking into Java's implementation if you need that specifically.

Ninefingers
The `\U` in your replacement string will cause a RuntimeException to be thrown.
Bart Kiers
Thanks. I've been pretty impressed at how much the regexp syntax is identical, aside from extra backslashing in Java. (Too bad about \U, though.) I wish Java's string handling was more like Perl's....
reid
As it comes to string manipulation, Perl is king. But yes, Java's regex implementation is a solid and powerful one, albeit verbose at times with all the backslashes and object creation.
Bart Kiers
\U isn't really regexp syntax in perl, it's double-quoted string syntax.
ysth