regex - How to extract "StrB(StrC,StrD)(StrE)" from "StrA(StrB(StrC,StrD)(StrE)) StrF " in C#? -
regex - How to extract "StrB(StrC,StrD)(StrE)" from "StrA(StrB(StrC,StrD)(StrE)) StrF " in C#? -
i tried utilize this:regex.match(input, @"\(([^)]*)\)")
, give me "(strb(strc,strd)" not want.
i want extract string between 2 parentheses, string within can have own set of parentheses nested within main 2 parentheses , string can infinitely nested parentheses like:
"a(b(c(d(...))))"
, thought how done? thanks.
this want:
var regex = new regex( @"(?<=\()(?:[^()]|(?<br>\()|(?<-br>\)))*(?(br)(?!))(?=\))"); var input = "stra(strb(strc,strd)(stre)) strf"; var matches = regex.matches(input);
the regular look breaks downwards follows:
(?<=\() preceeded ( (?: don't bother capturing grouping [^()]+ match 1 or more non-brackets | or (?<br>\() capture (, increment br count | or (?<-br>\)) capture ), decrement br count or fail if it's 0 (failing here mean we've reached end of our match) ) * 0 or more times (?(br)(?!)) fail if there's br count greater 0 (literally, if it's greater zero, match (?!); (?!) means 'not followed ""', fails) (?=\)) succeeded )
c# regex string
Comments
Post a Comment