c# - extracting a list of strings that match a pattern for the beginning of each string -
i have pretty long string want extract strings match pattern beginning of each string. example, suppose have this:
string thelongstring = lorem ipsum "somewordone" dolor sit "somewordtwo" amet;
how extract string strings in quotes , start someword? instance, in case, list should contain "somewordone"
, "somewordtwo"
.
thanks.
assuming pattern searching not single word (kd.'s answer work that), use regex match want:
regex regex = new regex("\"someword[^\"]*\""); var matches = regex.matches(thelongstring); list<string> mymatchedstrings = new list<string>(); foreach (match match in matches) { mymatchedstrings.add(match.value); }
if don't want double quotes included in results, use following drop-in replacement uses regex behind find opening quote, won't show in result:
regex regex = new regex("(?<=\")someword[^\"]*");
or if wanted exclude "someword" result:
regex regex = new regex("(?<=\"someword)[^\"]*");
that should leave mymatchedstrings
list of pattern, long pattern starts , ends double quotes , starts "someword", can adjust necessary.
Comments
Post a Comment