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 here
should 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
Post a Comment