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
!
This article is visually unappealing due to the formatting of dev so I am also linking my blog where it looks better.
^
: 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\<
.\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!*
: 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.
: Exactly 3. The perfect number of times to do something.
: 3 or more. Like a marathon that never seems to end.
: 2, 3, 4 or 5. Like a multiple-choice question with several correct answers.\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.?=
: 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.()
: 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. 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.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.^\w+
^.*\n.*\n.*$
\b\w*z\w*\b
\b[^e\s]+\b
^\d.*$
^.*\p$
(\d)-(\d)-(\d)
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]\b
^\bLorem\b.*$
^.*\b\d$
^(?=.*regex)(?=.*cheat).*$
^(?!.*Python).*$
^\b(\w+)\b.*\b\1\b$
^.$
^.*\b\d\b.*$
^.*\b[aeiou]\w*\b.*$
^.*\b[^aeiou]\w*\b.*$
^.*\bc\w*t\b.*$