GWT: how to get the regular expression (Pattern and Matcher), working on the client side

We use GWT 2.03 together with SmartGWT 2.2. I am trying to match a regex as shown below in client code.

Pattern pattern = Pattern.compile("\\\"(/\d+){4}\\\"");
String testString1 = "[    \"/2/4/5/6/8\",    \"/2/4/5/6\"]";
String testString2 = "[  ]";

Matcher matcher = pattern.matcher(testString1);
boolean result = false;
while (matcher.find()) {
    System.out.println(matcher.group());
}

It seems that the Pattern and Matcher classes are NOT compiled in Javascript by the GWTC compiler, and therefore this application did NOT load. What is the equivalent GWT client code so that I can find regular expression matches within a string?

How could you match regular expressions inside String on the GWT side on the client side?

Thank,

+5
source share
5 answers

Consider upgrading to GWT 2.1 and using RegExp.

+11

String ! :

String text = "google.com";
    if (text.matches("(\\w+\\.){1,2}[a-zA-Z]{2,4}"))
        System.out.println("match");
    else
        System.out.println("no match");

, , - . .

,

+12

GWT JSNI Javascript regexp:

public native String jsRegExp(String str, String regex)
/*-{
    return str.replace(/regex/);  // an example replace using regexp 
    }
}-*/;
+5

, RegExp GWT 2.1 ?

http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/regexp/

GWT 2.1 , - , "RegExp.gwt.xml" <inherits> XML GWT.

I'm not sure if this will work, but it is worth it. It may be referring to something else GWT 2.1 that you don’t have, but I just checked the code a bit and don't think so.

+2
source

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

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

if (matchFound) {
Window.alert("Match found");
    // Get all groups for this match
    for (int i=0; i<=matcher.getGroupCount(); i++) {
        String groupStr = matcher.getGroup(i);
        System.out.println(groupStr);
    }
}else{
Window.alert("Match not found");
}
+2
source

All Articles