Regular Expression: Difference between revisions

From Ideasplace
Jump to navigation Jump to search
Line 1: Line 1:
==Basic RegEx Cheat Sheet==
{| class="wikitable"
{| class="wikitable"
! Symbol !! Name !! Meaning !! Example !! Matches
! Category !! Symbol !! Name !! Meaning !! Example !! Matches
|-
|-
! colspan="5" style="text-align:center;" | Literals
! colspan="6" style="text-align:center;" | 1. Wildcards & Character Classes
|-
|-
| rowspan="6" | '''Matching Characters'''
| (text) || Literal || Matches exact text || <code>cat</code> || "cat"
| (text) || Literal || Matches exact text || <code>cat</code> || "cat"
|-
|-
! colspan="5" style="text-align:center;" | Wildcards & Classes
| <code>.</code> || Dot || Matches '''any''' single character (except newline) || <code>h.t</code> || "hat", "hot"
|-
| <code>.</code> || Dot || Matches '''any''' single character || <code>h.t</code> || "hat", "hot"
|-
|-
| <code>[ ]</code> || Brackets || List of allowed characters || <code>b[ai]t</code> || "bat", "bit"
| <code>[ ]</code> || Brackets || List of allowed characters || <code>b[ai]t</code> || "bat", "bit"
Line 16: Line 13:
| <code>[^ ]</code> || Negation || Matches anything '''NOT''' in the list || <code>[^a]</code> || Any char except 'a'
| <code>[^ ]</code> || Negation || Matches anything '''NOT''' in the list || <code>[^a]</code> || Any char except 'a'
|-
|-
! colspan="5" style="text-align:center;" | Quantifiers
| <code>\d</code> || Digit || Any number 0-9 || <code>\d\d</code> || "42", "99"
|-
| <code>\w</code> || Word Char || Letter, number, or underscore || <code>\w+</code> || "User_1"
|-
| <code>\s</code> || Space || Whitespace (space, tab, enter) || <code>A\sB</code> || "A B"
|-
! colspan="6" style="text-align:center;" | 2. Quantifiers (How Many?)
|-
|-
| <code>*</code> || Star || '''0''' or more times || <code>go*d</code> || "gd", "god", "good"
| rowspan="7" | '''Counting'''
| <code>*</code> || Star || '''0''' or more times || <code>go*d</code> || "gd", "god"
|-
|-
| <code>+</code> || Plus || '''1''' or more times || <code>go+d</code> || "god", "good"
| <code>+</code> || Plus || '''1''' or more times || <code>go+d</code> || "god", "good"
Line 24: Line 28:
| <code>?</code> || Question || '''0''' or '''1''' time (Optional) || <code>colou?r</code> || "color", "colour"
| <code>?</code> || Question || '''0''' or '''1''' time (Optional) || <code>colou?r</code> || "color", "colour"
|-
|-
! colspan="5" style="text-align:center;" | Anchors
| <code>{n}</code> || Exact Count || Matches exactly n times || <code>\d{3}</code> || "123"
|-
| <code>{n,}</code> || Min Count || Matches n or more times || <code>\d{3,}</code> || "123", "12345"
|-
| <code>{n,m}</code> || Range || Matches between n and m times || <code>\d{2,4}</code> || "12", "1234"
|-
| <code>.*?</code> || Lazy Match || Matches as little as possible || <code><.*?></code> || Stops at first matching char
|-
! colspan="6" style="text-align:center;" | 3. Anchors & Boundaries (Where?)
|-
|-
| <code>^</code> || Caret || Start of string || <code>^The</code> || "The end"
| rowspan="3" | '''Positioning'''
| <code>^</code> || Caret || Start of string/line || <code>^The</code> || Starts with "The"
|-
|-
| <code>$</code> || Dollar || End of string || <code>end$</code> || "The end"
| <code>$</code> || Dollar || End of string/line || <code>end$</code> || Ends with "end"
|-
|-
! colspan="5" style="text-align:center;" | Special Characters
| <code>\b</code> || Word Boundary || The edge of a word (start or end) || <code>\bcat\b</code> || "cat" (not "scat")
|-
|-
| <code>\</code> || Escape || Treats special characters as text || <code>\.</code> || "." (actual dot)
! colspan="6" style="text-align:center;" | 4. Groups & Logic
|-
|-
| <code>\</code> || Escape || Treats special characters as text || <code>\$</code> || "$" (actual dollar)
| rowspan="2" | '''Logic'''
| <code>|</code> || Pipe || OR logic (Alternative) || <code>cat|dog</code> || "cat" or "dog"
|-
|-
| <code>\d</code> || Digit || Any number 0-9 || <code>\d\d</code> || "42", "99"
| <code>(...)</code> || Group || Groups characters or captures them || <code>(a|b)c</code> || "ac" or "bc"
|-
|-
| <code>\w</code> || Word || Letter, number, or underscore || <code>\w+</code> || "Hello_1"
! colspan="6" style="text-align:center;" | 5. Special Characters
|-
|-
| <code>\s</code> || Space || Whitespace (space, tab, enter) || <code>A\sB</code> || "A B"
| '''Escaping''' || <code>\</code> || Backslash || Treats special characters as text || <code>\.</code> || "." (actual dot)
|}
|}

Revision as of 17:45, 23 January 2026

Category Symbol Name Meaning Example Matches
1. Wildcards & Character Classes
Matching Characters (text) Literal Matches exact text cat "cat"
. Dot Matches any single character (except newline) h.t "hat", "hot"
[ ] Brackets List of allowed characters b[ai]t "bat", "bit"
[^ ] Negation Matches anything NOT in the list [^a] Any char except 'a'
\d Digit Any number 0-9 \d\d "42", "99"
\w Word Char Letter, number, or underscore \w+ "User_1"
\s Space Whitespace (space, tab, enter) A\sB "A B"
2. Quantifiers (How Many?)
Counting * Star 0 or more times go*d "gd", "god"
+ Plus 1 or more times go+d "god", "good"
? Question 0 or 1 time (Optional) colou?r "color", "colour"
{n} Exact Count Matches exactly n times \d{3} "123"
{n,} Min Count Matches n or more times \d{3,} "123", "12345"
{n,m} Range Matches between n and m times \d{2,4} "12", "1234"
.*? Lazy Match Matches as little as possible <.*?> Stops at first matching char
3. Anchors & Boundaries (Where?)
Positioning ^ Caret Start of string/line ^The Starts with "The"
$ Dollar End of string/line end$ Ends with "end"
\b Word Boundary The edge of a word (start or end) \bcat\b "cat" (not "scat")
4. Groups & Logic
Logic Pipe OR logic (Alternative) dog "cat" or "dog"
(...) Group Groups characters or captures them b)c "ac" or "bc"
5. Special Characters
Escaping \ Backslash Treats special characters as text \. "." (actual dot)