Files
Snippets/Regular Expressions Cheat Sheet.md
T
2026-09-17 08:12:04 +02:00

12 KiB
Raw Blame History

Regular Expressions Cheat Sheet

A regular expression specifies a set of strings that matches it. This cheat sheet is based off Python 3's Regular Expressions (http://docs.python.org/3/library/re.html) but is designed for searches within Sublime Text.

https://gist.github.com/IsaacCisneros/d7f3cf4bf6c0573d0570

https://ahkde.github.io/docs/v1/misc/RegEx-QuickRef.htm

https://serverdiscounter.com/blog/2026-01-26-regex-grundlagen

Special Characters

\ Escapet Sonderzeichen oder kennzeichnet eine spezielle Sequenz
. Entspricht einem beliebigen einzelnen Zeichen mit Ausnahme eines Zeilenumbruchs
^ Matches the start of the string.
$ Matches the end of the string​
* Greedily matches 0 or more repetitions of the preceding RE.
*? Matches 0 or more repetitions of the preceding RE.
+ Greedily matches 1 or more repetitions of the preceding RE.
+? Matches 1 or more repetitions of the preceding RE.
? Greedily matches 0 or 1 repetitions of the preceding RE.
?? Matches 0 or 1 repetitions of the preceding RE.
A|B Matches A, if A is unmatched then matches B, where A and B are arbitrary REs.
{m} Matches exactly m many repetitions of the previous RE.
{m,n} Greedily matches from m many to n many repetitions of the previous RE.
{m,n}? Matches m many to n many repetitions of the previous RE.
[...] Indicates a set of characters to match.
[amk] Matches 'a', 'm', or 'k'.
[a-z] Matches 'a' through 'z'.
[a-f0-7] Matches 'a' through 'f' or '0' through '7'.
[a\-z] Matches 'a', '-', or 'z'.
[a-] Matches 'a' or '-'.
[-a] Matches 'a' or '-'.
[(+*)] Matches '(', '+', '*', or ')'. [] matches special characters literally.

​

[\w]                Matches the character class for '\w'. See character classes.
[^5]                Matches anything other than '5'. '^' forms the complementary set only as the first character in a set.
[]()]                Matches ']', '(', and ')'. ']' is taken literally only as the first character in a set.
[()\]]                Matches ']', '(', and ')'.

(...) Matches the RE inside the parenthesis and assigns a new group. (?P...) The RE matched is accessible by the group indicated by name.

(?...) Extension notation which changes a RE's behavior. These do not assign a new group. (?aiLmsux) Sets the corresponding flag to each letter. Does not work within Sublime Text. (?:...) A non-capturing version of parenthesis. The matched substring cannot be retrieved later. (?P=name) Matches the substring matched by the group named name. (?#...) A comment, the contents are ignored. (?=...) Lookahead assertion, the preceding RE only matches if this matches. (?!...) Negative lookahead assestion, the preceding RE only matches if this doesn't match. (?<=...) Positive lookbehind assertion, the following RE will only match if preceeded with this fixed length RE. (?<!...) Negative lookbehind assertion, the following RE will only match if not preceeded with this fixed length RE. (?(id)true|false) If group id exists then uses the true RE, else use the false RE.

Character classes \1 Matches the contents of the group labelled by the same number. Acceptable numbers are 1-99. \A Matches at the start of the current string. \b Matches the empty string at the beginning or end of a word. \b matches the boundary between \w and \W. \B Matches the empty string not at the beginning or end of a word. \d Matches any Unicode decimal digit, including 0-9. \D Matches any Unicode non-decimal digit. \s Matches any Unicode whitespace character, including ' ', \t, \n, \r, \f and \v. \S Matches any Unicode non-whitespace character. \w Matches any Unicode word character, including a-z, A-Z, and 0-9. \W Matches any Unicode non-word character. \Z Matches at the end of the string.

\a Matches the ASCII Bell (). \f Matches the ASCII Formfeed ( ). \n Matches the ASCII Linefeed. \r Matches the ASCII Carriage Return ( ). \t Matches the ASCII Horizontal Tab. \v Matches the ASCII Vertical Tab ( ).

#########################

Regular Expression Cheat Sheet with Examples

Regular Expressions (regex) are patterns used to match strings in text. Below is a cheat sheet with common regex patterns and examples.

1. Anchors

Pattern Description Example Matches
^ Start of the string ^Hello Hello world!
$ End of the string world$ Hello world!

2. Quantifiers

Pattern Description Example Matches
* 0 or more a* aaa, b
+ 1 or more a+ aaa, but not b
? 0 or 1 colou?r color, colour
{n} Exactly n occurrences a{3} aaa, but not aa
{n,} At least n occurrences a{2,} aa, aaa
{n,m} Between n and m occurrences a{2,3} aa, aaa, but not a

3. Character Classes

Pattern Description Example Matches
. Any character except newline a.b acb, a9b
\d Any digit (0-9) \d+ 123, 9
\D Any non-digit \D+ abc, @#
\w Any word character (alphanumeric or _) \w+ hello_123
\W Any non-word character \W+ @!, #
\s Any whitespace \s+ , \t, \n
\S Any non-whitespace \S+ abc, 123

4. Logical OR and Grouping

Pattern Description Example Matches
`a b` Match either a or b `cat
`(ab cd)` Grouping `(ab

5. Escape Characters

Pattern Description Example Matches
\. Literal dot a\.b a.b
\[ Literal [ \[abc\] [abc]

6. Ranges

Pattern Description Example Matches
[a-z] Lowercase letters [a-z]+ abc, xyz
[A-Z] Uppercase letters [A-Z]+ ABC, XYZ
[0-9] Digits [0-9]+ 123, 456
[a-zA-Z0-9_] Alphanumeric and underscore \w+ hello123_

7. Special Sequences

Pattern Description Example Matches
(?=...) Positive lookahead foo(?=bar) foo in foobar
(?!...) Negative lookahead foo(?!bar) foo in foobaz
(?<=...) Positive lookbehind (?<=foo)bar bar in foobar
(?<!...) Negative lookbehind (?<!foo)bar bar in bazbar

8. Common Patterns

Use Case Regex Example
Match email [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} user@example.com
Match URL https?://[^\s]+ https://example.com
Match phone number \d{3}-\d{3}-\d{4} 123-456-7890
Match date (YYYY-MM-DD) \d{4}-\d{2}-\d{2} 2024-12-13

9. Flags

Flag Description Example
i Case-insensitive /hello/i matches Hello
g Global match /hello/g matches all hello
m Multiline /^hello/m matches per line

10. Examples in Code

Python Example:

import re
# Match an email
pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
email = "contact@example.com"
print(re.match(pattern, email))  # Output: <re.Match object>

JavaScript Example:

// Match a URL
let pattern = /https?:\/\/[^\s]+/g;
let text = "Visit https://example.com for more info.";
console.log(text.match(pattern)); // Output: [ 'https://example.com' ]

regular-expressions-v2.pdf

regular-expressions-cheat-sheet-v1.pdf

regex-cheatsheet.pdf