6

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.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
reid
  • 63
  • 4
  • 3
    Have a look at the Jakarta ORO regexp library, for \U support see the http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html class – rsp Feb 12 '10 at 21:25
  • After looking into it a little bit, ORO is indeed very useful. Having s/// and a built-in \U operator is great. So far, I recommend it! – reid Feb 16 '10 at 20:04

3 Answers3

6

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
  • 14,018
  • 5
  • 37
  • 49
1

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

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thanks. I've also found Matcher.replaceAll(). Are they powerful enough to handle my example of upcasing the match, though...? – reid Feb 12 '10 at 20:38
  • `Matcher.replaceAll()` is used by `String`'s `replaceAll()`. So that answers your other question: no, `\U` is not supported. – Bart Kiers Feb 12 '10 at 20:40
  • 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 Feb 12 '10 at 20:40
  • To emphasize: not only `replaceAll(...)` does not support it, but the entire Java regex package doesn't. – Bart Kiers Feb 12 '10 at 20:45
  • Thanks again, Bart (and to everyone else, too; my first question here!). – reid Feb 12 '10 at 21:11
1

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.

  • The `\U` in your replacement string will cause a RuntimeException to be thrown. – Bart Kiers Feb 12 '10 at 20:41
  • 1
    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 Feb 12 '10 at 20:45
  • 1
    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 Feb 12 '10 at 20:51
  • 1
    \U isn't really regexp syntax in perl, it's double-quoted string syntax. – ysth Feb 12 '10 at 22:43