PDA

View Full Version : GWT Regex



LINEMAN78
27 Mar 2009, 4:40 PM
I know this is probably a stupid question, but I cannot find a solution anywhere. Is there a way to perform regex on a string and have access to the groups? I really don't want to have to make a server call in order to parse the string. I am simply trying to parse a phone number into the 3 distinct sections. Below is how I would implement it it I were to do it in Java:



Pattern phonePattern = Pattern.compile("^((\\((\\d{3})\\)(\\d{3})-(\\d{4}))|((\\d{3})-(\\d{3})-(\\d{4})))$");

String voiceNum = (String)phoneNumberItem.getValue();
String[] voiceNumSplit = new String[3];
Matcher voiceMatcher = phonePattern.matcher(voiceNum);
for (int i = 0; i < voiceMatcher.groupCount(); i++)
Window.alert("Group "+i+": "+voiceMatcher.group(i));

ydi
1 Sep 2010, 1:15 AM
Hi,
I have the same problem.
Will you tell me if you found a solution.
Thank you

LINEMAN78
1 Sep 2010, 8:05 AM
Depends on how complicated you want to make it. This is the simple solution I am using, it simply returns an array of the groups:


private native JsArrayString match(String searchString, String regex)
/*-{
return searchString.match(regex);
}-*/;

Here is a more complicated solution:
http://www.java2s.com/Code/Java/GWT/ImplementjavautilregexPatternwithJavascriptRegExpobject.htm