.net - C# Regex.Match curly brackets- contents only? (exclude braces) -
i've been unable find answer on this: can use regex.matches method return contents of items curly braces?
if use regex ({[^}]*}) matchcollection values includes braces. want match, return contents. here's have far:
regex regex = new regex(({[^}]*}), regexoptions.ignorecase); matchcollection matches = regex.matches("test {token1} {token 2}"); // results include braces (undesirable) var results = matches.cast<match>().select(m => m.value).distinct().tolist();
i liked explicit. can use "positive lookbehind" (?<=...) , "positive lookahead" (?=...) groups:
(?<=\{) [^}]* (?=\}) which means:
- require opening curly bracket before match
- collect text (of, course) - commented before may [^{}]* well
- require closing curly bracket after match
Comments
Post a Comment