java - RegExp and MatchResult in GWT returns only first match -
i trying use regexp , matchresult in gwt. returns first occurrence in word. need have 3 "g", "i", "m". tried "gim" global, multiline , case insensitive. not working. please find code below. in advance.
the expected output is, should find 3 matches of "on" in "on condition" irrespective of case.
import com.google.gwt.regexp.shared.matchresult; import com.google.gwt.regexp.shared.regexp; public class patternmatchingingxt { public static final string dtovalue = "on condition"; public static final string searchterm = "on"; public static void main(string args[]){ string newdtodata = null; regexp regexp = regexp.compile(searchterm, "mgi"); if(dtovalue != null){ matchresult matcher = regexp.exec(dtovalue); boolean matchfound = matcher != null; if (matchfound) { (int = 0; < matcher.getgroupcount(); i++) { string groupstr = matcher.getgroup(i); newdtodata = matcher.getinput().replaceall(groupstr, ""+i); system.out.println(newdtodata); } } } } }
if need collect matches, run exec
until no match.
to replace multiple occurrences of search term, use regexp#replace()
pattern wrapped capturing group (i not make $&
backreference whole match work in gwt).
change code follows:
if(dtovalue != null){ // display matches regexp regexp = regexp.compile(searchterm, "gi"); matchresult matcher = regexp.exec(dtovalue); while (matcher != null) { system.out.println(matcher.getgroup(0)); // print match value (demo) matcher = regexp.exec(dtovalue); } // wrap searchterm occurrences 1 , 0 regexp regexp1 = regexp.compile("(" + searchterm + ")", "gi"); newdtodata = regexp1.replace(dtovalue, "1$10"); system.out.println(newdtodata); // => 1on0 c1on0diti1on0 }
note m
(multiline modifier) affects ^
, $
in pattern, thus, not need here.
Comments
Post a Comment