Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT : how to get regex(Pattern and Matcher) working in client side

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,

like image 241
anjanb Avatar asked Nov 11 '10 19:11

anjanb


3 Answers

Just 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

like image 92
glenn Avatar answered Sep 22 '22 21:09

glenn


Consider upgrading to GWT 2.1 and using RegExp.

like image 32
Adrian Smith Avatar answered Sep 24 '22 21:09

Adrian Smith


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 
    }
}-*/;
like image 20
Peter Knego Avatar answered Sep 25 '22 21:09

Peter Knego