java - Get all matches within a string using complie and regex -


i'm trying matches starts _ , ends = url looks like

?_field1=param1,param2,paramx&_field2=param1,param2,paramx

in case i'm looking instance of _fieldx=

a method use looks like

public static list<string> getallmatches(string url, string regex) {     list<string> matches = new arraylist<string>();     matcher m = pattern.compile("(?=(" + regex + "))").matcher(url);     while(m.find()) {         matches.add(m.group(1));     }     return matches; } 

called

list<string> fieldslist = getallmatches(url, "_.=");

but somehow not finding have expected.

any suggestions have missed?

a regex (?=(_.=)) matches occurrences of overlapping matches start _, have 1 char (other line break char) , =.

you need no overlapping matches in context of string provided.

you may use lazy dot matching pattern, _(.*?)=. alternatively, may use negated character class based regex: _([^=]+)= (it capture group 1 1 or more chars other = symbol).


Comments