Regular Expressions
Regex.IsMatch(input, regex[, RegexOptions.Multiline]);
Match m = Regex.Match(input, regex);
m.Groups[i];
while(m.Success) {
m.NextMatch();
m.Result(replaceExpression);
Regex.Replace(input, regex, replaceExpression);
}
RegexOptions
| RegexOptions.None | |
| RegexOptions.IgnoreCase | (i) |
| RegexOptions.Multiline | Changes meaning of ^ and $ to match beginning and end of each line rather than whole string. |
| RegexOptions.ExplicitCapture | (n) - Specifies that the only valid captures are explicitly named or numbered groups of the form (?&nblt;name&nbgt;?). This allows unnamed parentheses to act as noncapturing groups without the syntactic clumsiness of the expression (?:?). |
| RegexOptions.Compiled | Specifies that the regular expression is compiled to an assembly. This yields faster execution but increases startup time. |
| RegexOptions.SingleLine | (s) - . matches \n |
| RegexOptions.IgnorePatternWhitespace | (x) and allows comments marked with # |
| RegexOptions.RightToLeft | |
| RegexOptions.CultureInvariant | Specifies that cultural differences in language is ignored. |
MetaCharacters
| ^ | start (of each line if multiline) |
| $ | end (of each line if multiline) |
| \A | absolute start |
| \Z | absolute end (ignoring last \n) |
| \z | absolute end |
| \G | match after where last match ended |
| \b | word boundary (between \w & \W) |
| \B | non-word boundary |
| \t | tab |
| \x20 | ASCII |
| \cC | Control C |
| \u0020 | Unicode |
| * | 0-∞ |
| + | 1-∞ |
| ? | 0-1 |
| {n} | Match n times |
| {n,m} | Match between n and m times |
| The above appended with ? | Non-greedy |
| . | Anything but \n |
| x|y | x or y |
| [xyz] | |
| [a-z] | |
| \d | digit |
| \D | non-digit |
'| \s | white space character |
| \S | non-white space character |
| \w | [A-Za-z0-9_] |
| \W | Opposite of \w |
| (?:[xyz]) | Non-capturing |
| (?<groupName>[xyz]) | named group |
| (?<myGroup>\s\w+)\k<myGroup> | Backreference, matches "the the", "the theory" |
Substitutions
\1 matches first group
$n
${name}
$$ - $ sign