Regex Cheatsheet 101
Are you tired of trying to make sense of regex? Don't worry, you're not alone! Regex is like a secret language that only a select few seem to understand.
We've got you covered with our Regex Cheatsheet 101!
NOTE: This blog is visually unappealing due to the formatting of dev so I am also linking my blog where it looks better.
Anchors
^: Start of string or line. Think of it as a hat for your text. It marks the beginning of a line.
\A: Start of string. A fancier hat that marks the beginning of a string.
$: End of string or line. The opposite of^, it marks the end of a line.
\Z: End of string. The fancier cousin of$that marks the end of a string.
\b: Word boundary. It's like a little force field around a word that tells you where it begins and ends.
\B: Not word boundary. The opposite of\b.
\<: Start of word. Similar to^and\A, but specifically for words.
\>: End of word. The opposite of\<.
Character Classes
\c: Control character. A character that controls your text. Watch out, it's in charge!
\s: Whitespace. Spaces, tabs, and other invisible characters that make your text look nice and neat.
\S: Not whitespace. Anything that's not a space, tab, or other invisible character.
\d: Digit. The numbers 0-9.
\D: Not digit. Anything that's not a number.
\w: Word. Letters, numbers, and underscores.
\W: Not word. Anything that's not a letter, number, or underscore.
\x: Hexadecimal digit. A fancy name for numbers and letters (A-F, a-f) that are used in hex code.
\O: Octal digit. Numbers in base 8. Fun fact: octopuses are also good with digits!
Quantifiers
*: 0 or more. Like a buffet that you can go back to as many times as you want.
+: 1 or more. Like a VIP lounge that only lets in the cool kids who have something to offer.
?: 0 or 1 (optional). Like a checkbox that you can check or leave empty.
{3}: Exactly 3. The perfect number of times to do something.
{3,}: 3 or more. Like a marathon that never seems to end.
{2,5}: 2, 3, 4 or 5. Like a multiple-choice question with several correct answers.
Special Characters
\n: New line. The line break that you use when you want to start a new paragraph.
\r: Carriage return. A fancy name for the Enter key.
\t: Tab. The button that you press when you want to indent your text.
\v: Vertical tab. A rarely used character that you probably won't need to worry about.
\f: Form feed. A character that's mostly used in old printers. Don't worry about it.
Assertions
?=: Lookahead assertion. Like a crystal ball that tells you what's coming up next in your text.
?!: Negative lookahead. The opposite of?=. It tells you what's not coming up next in your text.
?<=: Lookbehind assertion. Like a time machine that takes you back to the beginning of a word or phrase.
?<!: Negative lookbehind. The opposite of?<=. It tells you what's not behind you in your text.
Groups and Capture
(): Group. A way to group parts of your regex together.
(?:): Non-capturing group. Like a regular group, but it doesn't create a capture group.
\n: Backreference. A way to reference a previous capture group.
(?P<name>): Named capture group. A way to give a capture group a name.
Alternation
|: Alternation. A way to match one thing or another. It's like a fork in the road.
[]: Character set. A way to match one character out of a set of characters.
[^]: Negated character set. The opposite of[]. Matches anything not in the set.
Flags
i: Case-insensitive. Matches both upper and lowercase letters.
m: Multiline. Allows^and$to match the beginning and end of lines.
s: Dot-all. Allows.to match newline characters.
x: Verbose. Allows you to write your regex in multiple lines and add comments.
Family Friendly Examples
- Match the first word of a sentence:
^\w+
- Match the whole text except for the last line:
^.*\n.*\n.*$
- Find all the words that contain the letter "z":
\b\w*z\w*\b
- Find all the words that do not contain the letter "e":
\b[^e\s]+\b
- Match any line that starts with a digit:
^\d.*$
- Match any line that ends with a punctuation mark:
^.*\p{P}$
- Find all the phone numbers in a text:
(\d{3})-(\d{3})-(\d{4})
- Find all the email addresses in a text:
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
- Match any line that starts with the word "Lorem":
^\bLorem\b.*$
- Match any line that ends with a three-digit number:
^.*\b\d{3}$
- Match any line that contains the word "regex" and the word "cheat":
^(?=.*regex)(?=.*cheat).*$
- Match any line that does not contain the word "Python":
^(?!.*Python).*$
- Match any line that starts and ends with the same word:
^\b(\w+)\b.*\b\1\b$
- Match any line that has more than 10 characters:
^.{11,}$
- Match any line that has exactly 3 digits:
^.*\b\d{3}\b.*$
- Match any line that has a word with 2 or more consecutive vowels:
^.*\b[aeiou]{2}\w*\b.*$
- Match any line that has a word with at least 3 consonants in a row:
^.*\b[^aeiou]{3}\w*\b.*$
- Match any line that has a word that starts with "c" and ends with "t":
^.*\bc\w*t\b.*$
SOCIAL SHARE CARD GENERATOR