1Regular Expression FUll Cheatsheet (For quick look) :)
2note: for downloading visit https://buggyprogrammer.com/regular-expression-cheatsheet/
3------x-------------------x----------------x------------x----------------x-------------
4
5# -------------- Anchors --------------
6^ → Start of string, or start of line in multiline pattern
7\A → Start of string
8$ → End of string, or end of line in multi-line pattern
9\Z → End of string
10\b → Word boundary
11\B → Not word boundary
12\< → Start of word
13\> → End of word
14
15
16# ---------- Character Classes --------
17\c → Control character
18\s → White space
19\S → Not white space
20\d → Digit
21\D → Not digit
22\w → Word
23\W → Not word
24\x → Hexadecimal digit
25\O → Octal digit
26
27
28# --------- Quantifiers -----------------
29* → 0 or more
30{3} → Exactly 3
31+ → 1 or more
32{3,} → 3 or more
33? → 0 or 1
34{3,5} → 3, 4 or 5
35Add a ? to a quantifier to make it ungreedy.
36
37
38# ------- Special Characters -------------
39\n → New line
40\r → Carriage return
41\t → Tab
42\v → Vertical tab
43\f → Form feed
44\xxx → Octal character xxx
45\xhh → Hex character hh
46
47
48# --------- Groups and Ranges -------------
49. → Any character except new line (\n)
50(a|b) → a or b
51(...) → Group
52(?:...) → Passive (non-capturing) group
53[abc] → Range (a or b or c)
54[^abc] → Not (a or b or c)
55[a-q] → Lower case letter from a to q
56[A-Q] → Upper case letter from A to Q
57[0-7] → Digit from 0 to 7
58\x → Group/subpattern number "x"
59Ranges are inclusive.
60
61
62# ----------- Assertions ---------------
63?= → Lookahead assertion
64?! → Negative lookahead
65?<= → Lookbehind assertion
66?!= or ?<! → Negative lookbehind
67?> → Once-only Subexpression
68?() → Condition [if then]
69?()| → Condition [if then else]
70?# → Comment
71
72
73# ------ Pattern Modifiers --------
74g → Global match
75i* → Case-insensitive
76m* → Multiple lines
77s* → Treat string as single line
78x* → Allow comments and whitespace in pattern
79e* → Evaluate replacement
80U* → Ungreedy pattern
81* → PCRE modifier
82
83
84# ------ String Replacement ------
85$n → nth non-passive group
86$2 → "xyz" in /^(abc(xyz))$/
87$1 → "xyz" in /^(?:abc)(xyz)$/
88$` → Before matched string
89$' → After matched string
90$+ → Last matched string
91$& → Entire matched string
92Some regex implementations use \ instead of $
93
94
95# ---------- Escape Sequences ------------
96\ Escape following character
97\Q Begin literal sequence
98\E End literal sequence
99
100"Escaping" is a way of treating characters which have a special meaning in regular
101expressions literally, rather than as special characters.
102
103
104# --------- Common Metacharacters ---------
105^
106[
107.
108$
109{
110*
111(
112\
113+
114)
115|
116<
117>
118The escape character is usually \
119
120
121# ------------ POSIX ----------------
122[:upper:] → Upper case letters
123[:lower:] → Lower case letters
124[:alpha:] → All letters
125[:alnum:] → Digits and letters
126[:digit:] → Digits
127[:xdigit:] → Hexadecimal digits
128[:punct:] → Punctuation
129[:blank:] → Space and tab
130[:space:] → Blank characters
131[:cntrl:] → Control characters
132[:graph:] → Printed characters
133[:print:] → Printed characters and spaces
134[:word:] → Digits, letters and underscore
1Let regex;
2/* shorthand character classes */
3regex = /d/; // matches any digit, short for [0-9]
4regex = /D/; // matches non-digits, short for [^0-9]
5regex = /S/; // matches non-white space character
6regex = /s/; // matches any white space character
7regex = /w/; // matches character, short for [a-zA-Z_0-9]
8regex = /W/; // matches non-word character [^w]
9regex = /b/; // Matches a word boundary where a word character is [a-zA-Z0-9_]
10These meta characters boast a pre-defined meaning and make various typical patterns easier to use.
11/* matching using quantifiers */
12regex= /X./; // matches any character
13regex= /X*/; // Matches zero or several repetitions of letter X, is short for {0,}
14regex= /X+-/; // matches one or more repetitions of letter X, is short for {1,}
15regex= /X?/; // finds no or exactly one letter X, is short for is short for {0,1}.
16regex= // d{3}; // matches three digits. {} describes the order of the preceding liberal
17regex= // d{1,4} ; // means d must occur at least once and at a maximum of four
18A quantifies helps developers to define how often an element occurs.
19/* character ranges */
20regex = /[a-z]/; // matches all lowercase letters
21regex = /[A-Z]/; // matches all uppercase letters
22regex = /[e-l]/; // matches lowercase letters e to l (inclusive)
23regex = /[F-P]/; // matches all uppercase letters F to P (inclusive)
24regex = /[0-9]/; // matches all digits
25regex = /[5-9]/; // matches any digit from 5 to 9 (inclusive)
26regex = / [a-d1-7]/; // matches a letter between a and d and figures from 1 to 7, but not d1
27regex = /[a-zA-Z]/; // matches all lowercase and uppercase letters
28regex = /[^a-zA-Z]/; // matches non-letters
29/* matching using anchors */
30regex = / ^The/; // matches any string that starts with “The”
31regex = / end$/; // matches a string that ends with end
32regex = / ^The end$/; // exact string match starting with “The” and ending with “End”
33/* escape characters */
34regex = / a/; // match a bell or alarm
35regex = / e/; // matches an escape
36regex = / f/; // matches a form feed
37regex = / n/; // matches a new line
38regex = / Q…E/; // ingnores any special meanings in what is being matched
39regex = / r/; // matches a carriage return
40regex = / v/; // matches a vertical tab
41It is critical to note that escape characters are case sensitive
42/* matching using flags */
43regex = / i/; // ignores the case in pattern ( upper and lower case allowed)
44regex = / m/; // multi-line match
45regex = / s/; // match new lines
46regex = / x/; // allow spaces and comments
47regex = / j/; // duplicate group names allowed
48regex = / U/; // ungreedy match
1Let regex;
2/* shorthand character classes */
3regex = /d/; // matches any digit, short for [0-9]
4regex = /D/; // matches non-digits, short for [^0-9]
5regex = /S/; // matches non-white space character
6regex = /s/; // matches any white space character
7regex = /w/; // matches character, short for [a-zA-Z_0-9]
8regex = /W/; // matches non-word character [^w]
9regex = /b/; // Matches a word boundary where a word character is [a-zA-Z0-9_]
10These meta characters boast a pre-defined meaning and make various typical patterns easier to use.
11/* matching using quantifiers */
12regex= /X./; // matches any character
13regex= /X*/; // Matches zero or several repetitions of letter X, is short for {0,}
14regex= /X+-/; // matches one or more repetitions of letter X, is short for {1,}
15regex= /X?/; // finds no or exactly one letter X, is short for is short for {0,1}.
16regex= // d{3}; // matches three digits. {} describes the order of the preceding liberal
17regex= // d{1,4} ; // means d must occur at least once and at a maximum of four
18A quantifies helps developers to define how often an element occurs.
19/* character ranges */
20regex = /[a-z]/; // matches all lowercase letters
21regex = /[A-Z]/; // matches all uppercase letters
22regex = /[e-l]/; // matches lowercase letters e to l (inclusive)
23regex = /[F-P]/; // matches all uppercase letters F to P (inclusive)
24regex = /[0-9]/; // matches all digits
25regex = /[5-9]/; // matches any digit from 5 to 9 (inclusive)
26regex = / [a-d1-7]/; // matches a letter between a and d and figures from 1 to 7, but not d1
27regex = /[a-zA-Z]/; // matches all lowercase and uppercase letters
28regex = /[^a-zA-Z]/; // matches non-letters
29/* matching using anchors */
30regex = / ^The/; // matches any string that starts with “The”
31regex = / end$/; // matches a string that ends with end
32regex = / ^The end$/; // exact string match starting with “The” and ending with “End”
33/* escape characters */
34regex = / a/; // match a bell or alarm
35regex = / e/; // matches an escape
36regex = / f/; // matches a form feed
37regex = / n/; // matches a new line
38regex = / Q…E/; // ingnores any special meanings in what is being matched
39regex = / r/; // matches a carriage return
40regex = / v/; // matches a vertical tab
41It is critical to note that escape characters are case sensitive
42/* matching using flags */
43regex = / i/; // ignores the case in pattern ( upper and lower case allowed)
44regex = / m/; // multi-line match
45regex = / s/; // match new lines
46regex = / x/; // allow spaces and comments
47regex = / j/; // duplicate group names allowed
48regex = / U/; // ungreedy match
49
1import re
2reload (re)
3
4r = re.compile("([0-9]+)([a-zA-Z]+)([0-9]+)")
5m = r.match("123ab1234")
6if m:
7 print m.group(1)
8 print m.group(2)
9 print m.group(3)
10
11else:
12 print "no match"
13