We're using GWT 2.03 along with SmartGWT 2.2. I'm trying to match a regex like below in client side code.
Pattern pattern = Patternpile("\\\"(/\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 appears that Pattern and Matcher classes are NOT compiled to Javascript by the GWTC compiler and hence this application did NOT load. What is the equivalent GWT client code so that I can find regex matches within a String ?
How have you been able to match regexes within a String in client-side GWT ?
Thank you,
We're using GWT 2.03 along with SmartGWT 2.2. I'm trying to match a regex like below in client side 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 appears that Pattern and Matcher classes are NOT compiled to Javascript by the GWTC compiler and hence this application did NOT load. What is the equivalent GWT client code so that I can find regex matches within a String ?
How have you been able to match regexes within a String in client-side GWT ?
Thank you,
Share Improve this question asked Nov 11, 2010 at 19:01 anjanbanjanb 13.9k19 gold badges80 silver badges106 bronze badges5 Answers
Reset to default 14Just use the String class to do it! Like this:
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");
It works fine like this, without having to import or upgrade or whatever. Just change the text and regex to your liking.
Greetings, Glenn
Consider upgrading to GWT 2.1 and using RegExp
.
Use GWT JSNI to call native Javascript regexp:
public native String jsRegExp(String str, String regex)
/*-{
return str.replace(/regex/); // an example replace using regexp
}
}-*/;
Perhaps you could download the RegExp files from GWT 2.1 and add them to your project?
http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/regexp/
Download GWT 2.1 incl source, add that directory somewhere in your project, then add the reference to the "RegExp.gwt.xml" using the <inherits>
tag from your GWT XML.
I'm not sure if that would work, but it'd be worth a shot. Maybe it references something else GWT 2.1 specific which you don't have, but I've just checked out the code a bit and I don't think it does.
GWT 2.1 now has a RegExp class that might 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");
}