Basic Patterns:
- .: Any character except a newline.
- ^: Start of the line.
- $: End of the line.
- \b: Word boundary.
- \d: Digit (0-9).
- \D: Non-digit.
- \w: Word character (alphanumeric + underscore).
- \W: Non-word character.
- \s: Whitespace character.
- \S: Non-whitespace character.
Quantifiers:
- *: 0 or more occurrences.
- +: 1 or more occurrences.
- ?: 0 or 1 occurrence.
- {n}: Exactly n occurrences.
- {n,}: n or more occurrences.
- {n,m}: Between n and m occurrences.
Character Classes:
- [abc]: Any one of the characters a, b, or c.
- [a-z]: Any lowercase letter.
- [A-Z]: Any uppercase letter.
- [0-9]: Any digit.
Anchors:
- \bword\b: Word boundary for the word “word”.
- ^word: Line starts with “word”.
- word$: Line ends with “word”.
Grouping and Alternation:
- (abc): Grouping.
- a|b: Either a or b.
Escape Characters:
- : Escape character. Use it to match literal characters like . or .
Modifiers:
- i: Case-insensitive.
- m: Multi-line mode.
Quantifiers (non-greedy):
- *?: Match 0 or more, as few as possible.
- +?: Match 1 or more, as few as possible.
- ??: Match 0 or 1, as few as possible.
- {n,}?: Match at least n, as few as possible.
- {n,m}?: Match between n and m, as few as possible.
Common C# Regex Methods:
- Regex.IsMatch(input, pattern): Returns true if the input string matches the pattern.
- Regex.Match(input, pattern): Returns the first match.
- Regex.Matches(input, pattern): Returns all matches.
- Regex.Replace(input, pattern, replacement): Replaces matches with the specified