Table of Contents
- Description
- Cheatsheet from RegexOne (Shorter)
- Cheatsheet from Regex Learn (Longer)
- References
Description
Regex (regular expression) is a pattern-matching syntax used to search, extract, or manipulate text. It helps match specific text patterns like emails, dates, or URLs efficiently.
It is used in multiple languages, scripts and software. Super useful for universal find and replace needs. Works within Obsidian as well.
Cheatsheet from RegexOne (Shorter)
abc… - Letters
123… - Digits
\d - Any Digit
\D - Any Non-digit character
. - Any Character
\. - Period
[abc] - Only a, b, or c
[^abc] - Not a, b, nor c
[a-z] - Characters a to z
[0-9] - Numbers 0 to 9
\w - Any Alphanumeric character
\W - Any Non-alphanumeric character
{m} - m Repetitions
{m,n} - m to n Repetitions
* - Zero or more repetitions
+ - One or more repetitions
? - Optional character
\s - Any Whitespace
\S - Any Non-whitespace character
^…$ - Starts and ends
(…) - Capture Group
(a(bc)) - Capture Sub-group
(.*) - Capture all
(abc|def) - Matches abc or def
Cheatsheet from Regex Learn (Longer)
( starts **x** show what is selected by the command)
Flags
Ignore Case - i
Makes the expression case insensitive.
**CaT**Command: cat/ i
Global - g
Ensures that the expression does not stop on the first match.
**cat** **cat** **cat**Command: cat/ g
Multiline - m
If not enabled, line start and end equals the beginning and end of the entire string. It doesn’t work for each row individually.
**cat**
**cat**Command: cat/ m
Note: Further examples don’t have those flags written in their commands to keep the examples for transparent. Depending on the regex instance they should be selected or added at the end of the strings. Similarly as I don’t add / ad the beginning and end as well.
Character Classes
Character Set - [abc]
Matches any character in the set.
bar **ber** bir **bor** burCommand: `b[eo]r
Negated Character Set - [^abc]
Matches any character not in the set.
**bar** ber **bir** bor **bur**Command: b[^eo]r
Range - [a-z]
Matches all characters between two characters, including themselves.
abcd **e** **f** **g** **h** **i** jklmnopqrstuvwxyzCommand: [e-i]
Dot - .
Matches any character except line breaks.
**h** **i** ** ** **0** **1** **2** ** ** **_** **-** **!** **?**Command: .
Word - \w
Matches any alphanumeric character. Including the underline.
**h****i** **0****1****2** **_**-!?Command: \w
Not Word - \W
Matches any non-alphanumeric character.
hi** **012** **_**-****!****?**Command: \W
Digit - \d
Matches any numeric character.
+**1**-(**4****4****4**)-**2****2****2**Command: \d
Not Digit - \D
Matches any non-numeric character.
**+**1**-****(**444**)****-**222Command: \D
Whitespace - \s
Matches any whitespace character.
one** **twoCommand: \s
Not Whitespace - \S
Matches any non-whitespace character.
**o****n****e** **t****w****o**Command: \S
Quantifiers And Alternation
Plus - +
Expression matches one or more.
bp **bep** **beep** **beeep**Command: be+p
Asterisk - *
Expression matches zero or more.
**bp** **bep** **beep** **beeep**Command: be*p
Quantifier - {1,3}
Expression matches within specified ranges.
bp **bep** **beep** beeepCommand: be{1,2}p
Optional - ?
Makes the expression optional.
**color**, **colour**Command: colou?r
Alternation - |
Or it works like. It waits for one of the expressions it reserved to match.
fat, **cat**, **rat**Command: (c|r)at
Anchors
Start of string or line - ^
Matches the beginning of the string or line.
**an** answer or a questionCommand: ^\w+
End of string or line - $
Matches the end of the string or line.
an answer or a **question**Command: \w+$
Word Boundary - \b
Matches the word character or position at the end of a word.
an a**n**swer or a questio**n**Command: n\b
Not Word Boundary - \B
Matches a word character or position that is not at the end of a word.
an a**n**swer or a questionCommand: n\B
Group & References
Group - ()
Groups an expression. Can be used in programming to gather specific data.
**hahaha** **ha** h **haha**Command: (ha)+
Reference - \1
References a grouped expression. Allows to create variables. This doesn’t always in every regex implementation.
**hah** haa **dad**Command: (\w)a\1
Non Capturing Group - (?:)
Makes a grouping that cannot be referenced. This doesn’t always in every regex implementation.
**hahaha** **ha** h **haha**Command: (?:ha)+
Lookarounds
Positive Lookahead - (?=)
Matches a string the is followed by the specific text.
1st **2**nd 3pcCommand: \d(?=nd)
Negative Lookahead - (?!)
Matches a string the is not followed by the specified text.
**1**st 2nd **3**pcCommand: \d(?!nd)
Positive Lookbehind - (?<=)
Matches a string the has a specific text before itself.
#1 $2 %**3**Command: (?<=%)\d
Negative Lookbehind - (?<!)
Matches a string that does not have a specific text before itself.
#**1** $**2** %3Command: (?<!%)\d
References
https://regexlearn.com/
https://regexone.com/