0% found this document useful (0 votes)
4 views2 pages

Regex Cheatsheet

This document is a quick reference guide for regular expression syntax, covering anchors, character classes, quantifiers, groups, alternation, lookaround, common patterns, and the Python re module. It provides commands and patterns along with their descriptions for each category. This cheat sheet serves as a concise resource for understanding and utilizing regular expressions effectively.

Uploaded by

Rajesh Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Regex Cheatsheet

This document is a quick reference guide for regular expression syntax, covering anchors, character classes, quantifiers, groups, alternation, lookaround, common patterns, and the Python re module. It provides commands and patterns along with their descriptions for each category. This cheat sheet serves as a concise resource for understanding and utilizing regular expressions effectively.

Uploaded by

Rajesh Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Regex Cheat Sheet

Regular expression syntax quick reference

Anchors
Command / Pattern Description

^abc start of string/line

abc$ end of string/line

\b word boundary

\B not a word boundary

Character Classes
Command / Pattern Description

. any character except newline

\d \D digit / non-digit

\w \W word char / non-word char

\s \S whitespace / non-whitespace

[abc] a, b, or c

[^abc] not a, b, or c

[a-z0-9] range of characters

Quantifiers
Command / Pattern Description

a* 0 or more

a+ 1 or more

a? 0 or 1 (optional)

a{3} exactly 3

a{2,4} between 2 and 4

a{2,} 2 or more

a*? (lazy) non-greedy match

Groups & Alternation


Command / Pattern Description

(abc) capturing group

(?:abc) non-capturing group

(?P<name>abc) named group

a|b a or b

\1 backreference to group 1

Lookaround
Command / Pattern Description

(?=abc) positive lookahead

(?!abc) negative lookahead

(?<=abc) positive lookbehind

(?<!abc) negative lookbehind

Common Patterns
Command / Pattern Description

^[\w.-]+@[\w.-]+\.\w+$ basic email

^\d{3}-\d{3}-\d{4}$ phone number (US)

https?://\S+ URL

^\d{4}-\d{2}-\d{2}$ date YYYY-MM-DD

^[A-Za-z0-9_]{3,16}$ username, 3-16 chars

Python re Module
Command / Pattern Description

[Link](p, s) match at start of string

[Link](p, s) find first match anywhere

[Link](p, s) all non-overlapping matches

[Link](p, repl, s) replace matches

[Link](p, s) split string by pattern

[Link](p) precompile pattern for reuse

You might also like