DEV Community

Pradipta
Pradipta

Posted on • Updated on

Regular Expressions - Cheat Sheet

Metacharacters -

a. Wildcard - dot(.) matches any character except line breaks
b. Escaping - Backslash(\) allows use of metachacaters as literal characters
c. Special characters - Space, Tab(\t)

Character Sets -

[aeiou] - Matches one character from within the character set
[a-zA-Z0-9] - Character range
[^a-e] - Negative character set

Shorthand -
\d - [0-9]
\w - [a-zA-Z0-9_]
\s - Whitespace[\t\r\n]
\D - No digit
\W - Non word character
\S - Not Whitespace

Repetition -

  • - Preciding item, 0 or more times
  • - Preciding item, 1 or more times ? - Preciding item, 0 or 1 time

Ex : Good .+. - matches Good morning. Good day.
\d+ - Matches any number of characters
\s[a-z]+ed\s - Matches any word ending with ed.
It should have leading and trailing spaces.

Quantified repition -
{min, max}

\d{4,8} - 4 to 8 digits
\d{4} - Exactly 4 digits
\d{4,} - With 4 or more digits

Ex : \d{1}.\s\w{0,6}

a. a
b. ab
c. abc
d. abcd
e. abcde
f. abcdef

(+\d{1})\d{3}-\d{3}-*\d{4}
111-233-3455
1112333455
+1111-233-3455

Greedy Expression - Tries to match longest possible string

Lazy Expression - ? - Make preceding quantifier lazy

"(.|\n)+?" - Find anything within quotes including new line.

Grouping and Alteration

(in)?dependent - Matches independent and dependent
(abc)+ - Matches abc and abcabcabc

(\d{3})-(\d{3})-(\d{4}) - 555-666-8767
$1.($2).$3 - Gives 555.(666).8767

Alternation Metacharacter - |
apple|orange - Matches apple or orange

peanut(butter)? - Matches peanutbutter (Greedy)
peanut(butter)?? - Matches peanut (Lazy)

Anchors

^ - Start of String/Line
$ - End of String/Line
\A - Start of String, never end of line
\Z - End of String, never end of line

^\w+.\w+@\w+.[a-z]{3}$ - Email validation(^ and $ added to ensure it matches entire regular expression)
Word Boundary - \b - Word Boundary(Start/End of Word)
\B - Not a Word Boundary

Top comments (0)