java - How to obtain each group match in a regex repetition -


is there way obtain matches given group inside repetition (using java's built-in regex api)? if so, how?

here's example (live code):

pattern pattern = pattern.compile("if n = '([^']*)'( or n = '([^']*)')* then"); string script = "if n = 'abc' or n = 'def' or n = 'ghi' then"; matcher matcher = pattern.matcher(script);  for( int = 1; <= matcher.groupcount(); i++){     system.out.println(i + ": " + matcher.group(i)); } 

how obtain of individual matches group 3 (the part right of second = sign)? right now, loop above returns second match ("ghi").

if not possible, suggestions other methods welcomed, i'm looking simplest thing works. general use case matching against code snippets above fall set of categories. have regex's match each category, not easy way extract out important information matched groups.

i able achieve using capturing group around repetition , re-parsing full match repetition using second, smaller regex.

string script = "if n = 'abc' or n = 'def' or n = 'ghi' then";  // groups: // 1 = comparison value // 2 = "or" list //    1 = "or" comparison value pattern pattern = pattern.compile("if n = '([^']*)'(( or n = '([^']*)')*) then"); matcher matcher = pattern.matcher(script); system.out.println("matches: "+ matcher.matches()); system.out.println("if "+matcher.group(1));  // "or ..." parts string orlist = matcher.group(2); pattern innerpattern = pattern.compile(" or n = '([^']*)'"); matcher innermatcher = innerpattern.matcher(orlist); while(innermatcher.find()){     system.out.println("  or "+innermatcher.group(1)); } 

yields:

matches: true if abc   or def   or ghi 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -