java - extract multiple instances of a pattern occuring in string? -


i have string following:

string text = "this awesome                wait what?                [[foo:f1 ]]                [[foo:f2]]                [[foo:f3]]                texty text                [[foo:f4]] 

now, trying write function:

public string[] getfields(string text, string field){ // somethng  } 

enter code hereshould return [f1,f2,f3,f4] if pass text field = "foo"

how do cleanly?

use pattern:

pattern.compile("\\[\\[" + field + ":\\s*([\\w\\s]+?)\\s*\\]\\]"); 

and values of first capturing group.


string text = "this awesome wait what? [[foo:f1]] [[foo:f2]]"         + " [[foo:f3]] texty text [[foo:f4]]";  string field = "foo";  matcher m = pattern.compile(         "\\[\\[" + field + ":\\s*([\\w\\s]+?)\\s*\\]\\]").matcher(text);  while (m.find())     system.out.println(m.group(1)); 
 f1 f2 f3 f4 

you can put matches in list<string> , convert array.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -