c# - Regular expression, match anything not enclosed in -
given string foobarbarbarfoobar, want have between foo. used expression , result is: barbarbar. it's working great.
(?<=foo).*(?=foo)
now want opposite. given string foobarbarbarfoobar want not enclosed foo. tried following regular expression:
(?<!foo).*(?!foo)
i expected bar result instead returns match foobarbarbarfoobar. doesn't make sense me. missing?
the explanation from: https://regex101.com/ looks me?
(?<!foo).*(?!foo) (?<!foo) negative lookbehind - assert impossible match regex below foo matches characters foo literally (case sensitive) .* matches character (except newline) quantifier: * between 0 , unlimited times, many times possible, giving needed [greedy] (?!foo) negative lookahead - assert impossible match regex below foo matches characters foo literally (case sensitive)
any appreciated
i'm hoping finds better approach, abomination may want: (.*)foo(?<=foo).*(?=foo)foo(.*)
the text before first foo in capture group 1 (with provided example empty) , after in capture group 2 (would 'bar' in case)
if want 'foo's included on either end, use instead: (.*)(?<=foo).*(?=foo)(.*)
. result in 'foo' in group 1, , 'foobar' in group 2.
Comments
Post a Comment