c# - How do you regex this pattern? -
here pattern:
word [column] %anything goes%
basically, need extract "word", "column" , value between 2 "%" characters.
the following regular expression extracts each value named group.
(?<word>(.*))(\s*)\[(?<column>(.*))\]\s*%(?<anything>(.*))%
using on sample string should give following captures:
word = "word" column = "column" = "anything goes"
to run regular expression , values, can following (don't take best practise, it's illustrate basic concepts).
regex regex = new regex(@"(?<word>(.*))(\s*)\[(?<column>(.*))\]\s*%(?<anything>(.*))%"); match match = regex.match("word [column] %anything goes%"); console.writeline(match.groups["word"].value); console.writeline(match.groups["column"].value); console.writeline(match.groups["anything"].value);
this should output group captures console. more information on regular expressions in c#, check out msdn documentation regex
(this link other types may encounter). recommend expresso tool build , analyse regular expressions. expresso supports code emission , various other useful features.
Comments
Post a Comment