84

My questions is: Is there a good solution to use regular expression in GWT?

I'm not satisfied with the use of String.split(regex) for example. GWT translates the Code to JS and then uses the regex as a JS regex. But I cannot use something like the Java Matcher or Java Pattern. But I would need these for group matching.

Is there any possibility or library?

I tried Jakarta Regexp, but I had other problems because GWT doesn't emulate all methods of the Java SDK this library uses.

I want to be able to use something like this on the client side:

// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();

if (matchFound) {
    // Get all groups for this match
    for (int i=0; i<=matcher.groupCount(); i++) {
        String groupStr = matcher.group(i);
        System.out.println(groupStr);
    }
} 
Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
ludwigm
  • 3,363
  • 3
  • 28
  • 36

5 Answers5

101

The same code using RegExp could be:

// Compile and use regular expression
RegExp regExp = RegExp.compile(patternStr);
MatchResult matcher = regExp.exec(inputStr);
boolean matchFound = matcher != null; // equivalent to regExp.test(inputStr); 

if (matchFound) {
    // Get all groups for this match
    for (int i = 0; i < matcher.getGroupCount(); i++) {
        String groupStr = matcher.getGroup(i);
        System.out.println(groupStr);
    }
}
PhiLho
  • 40,535
  • 6
  • 96
  • 134
AleArnaiz
  • 1,026
  • 1
  • 8
  • 2
  • 2
    Are you sure that `<=` should not be `<` in `<=matcher.getGroupCount()`? – Andrey Regentov Dec 23 '13 at 11:14
  • @AndreyRegentov Yes. I checked, then fixed that. What is cool with these classes is that GWT supplies a pure Java version, so we can still test their usage with JUnit. – PhiLho Jul 24 '14 at 15:25
32

GWT 2.1 now has a RegExp class that might solve your problem:

Philippe Beaudoin
  • 3,290
  • 1
  • 22
  • 25
21

This answer covers ALL pattern matches, not only one, as in other answers here:

Function:

private ArrayList<String> getMatches(String input, String pattern) {
    ArrayList<String> matches = new ArrayList<String>();
    RegExp regExp = RegExp.compile(pattern, "g");
    for (MatchResult matcher = regExp.exec(input); matcher != null; matcher = regExp.exec(input)) {
       matches.add(matcher.getGroup(0));
    }
    return matches;
}

...and sample use:

ArrayList<String> matches = getMatches(someInputStr, "\\$\\{[A-Za-z_0-9]+\\}");
for (int i = 0; i < matches.size(); i++) {
    String match = matches.get(i);

}
Chris W
  • 1,562
  • 20
  • 27
  • 3
    You are absolutely right on this one. This is the missing piece of the puzzle. 10x alot! – gor Mar 19 '13 at 18:26
3

If you want a pure GWT solution, I'm not sure it can be done. But if you're willing to use JSNI, you can use JavaScript's RegExp object to get the matched groups and all. You'll need to learn JSNI for GWT and JavaScript RegExp object.

Raze
  • 2,175
  • 14
  • 30
0

The GWTx library seems to provide an emulation of java.util.regex.Pattern and friends. It doesn't look complete (Matcher in particular), but might be a good start.

The technique they use to plug their own implementations of Java classes for the client side is the <super-source> declaration in module XML. It's mentioned in GWT docs, module XML format description under "Overriding one package implementation with another". Standard JRE translatable classes in GWT are emulated the same way.

  • hi i tried this library before. it is a nice solution for missing JRe emulation, but in this case it doens't support grouping. The methods for this are missing. – ludwigm Jul 24 '09 at 10:12