Regex Cheat Sheet

In Regex

Regular expressions, or regex for short, are a powerful tool for searching and manipulating text. They allow you to specify patterns of characters that you want to match, making it easy to find specific pieces of information within large blocks of text.

Regex syntax can be a bit intimidating at first, but once you get the hang of it, you’ll wonder how you ever managed without it. To help you get started, we’ve created a cheat sheet that covers all of the basic regex syntax, as well as some more advanced techniques.

The cheat sheet is presented in a table format, with each row representing a different regex character or modifier. The first column describes the character or modifier, and the second column provides a brief explanation of what it does.

Cheat Sheet

CharacterDescription
.Matches any single character except for newline characters (\n).
\dMatches any digit character (0-9).
\DMatches any non-digit character.
\wMatches any word character (letters, digits, and underscores).
\WMatches any non-word character.
\sMatches any whitespace character (spaces, tabs, newlines, and carriage returns).
\SMatches any non-whitespace character.
[abc]Matches any one of the characters a, b, or c.
[^abc]Matches any character except for a, b, or c.
[a-z]Matches any lowercase letter from a to z.
[A-Z]Matches any uppercase letter from A to Z.
[0-9]Matches any digit from 0 to 9.
^Matches the beginning of a line.
$Matches the end of a line.
*Matches zero or more of the previous character or group.
+Matches one or more of the previous character or group.
?Matches zero or one of the previous character or group.
{n}Matches exactly n occurrences of the previous character or group.
{n,}Matches at least n occurrences of the previous character or group.
{n,m}Matches between n and m occurrences of the previous character or group.
``
()Groups multiple characters or expressions together to create a subexpression.
[]Specifies a character set to match against.

Here are some additional special characters and modifiers that you can use in regex:

CharacterDescription
\Escapes a special character so that it can be used as a literal character. For example, \. matches a period character.
.Matches any single character except for newline characters (\n).
*Matches zero or more of the previous character or group.
+Matches one or more of the previous character or group.
?Matches zero or one of the previous character or group.
{n}Matches exactly n occurrences of the previous character or group.
{n,}Matches at least n occurrences of the previous character or group.
{n,m}Matches between n and m occurrences of the previous character or group.
^Matches the beginning of a line.
$Matches the end of a line.
\bMatches a word boundary.
\BMatches a non-word boundary.
(?=…)Positive lookahead. Matches the preceding expression only if it is followed by the specified expression.
(?!…)Negative lookahead. Matches the preceding expression only if it is not followed by the specified expression.
(?<=…)Positive lookbehind. Matches the following expression only if it is preceded by the specified expression.
(?<!…)Negative lookbehind. Matches the following expression only if it is not preceded

Reference:

https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference