1>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
2>>> m[0] # The entire match
3'Isaac Newton'
4>>> m[1] # The first parenthesized subgroup.
5'Isaac'
6>>> m[2] # The second parenthesized subgroup.
7'Newton'
8
1// Javascript Regex Reference
2// /abc/ A sequence of characters
3// /[abc]/ Any character from a set of characters
4// /[^abc]/ Any character not in a set of characters
5// /[0-9]/ Any character in a range of characters
6// /x+/ One or more occurrences of the pattern x
7// /x+?/ One or more occurrences, nongreedy
8// /x*/ Zero or more occurrences
9// /x?/ Zero or one occurrence
10// /x{2,4}/ Two to four occurrences
11// /(abc)/ A group
12// /a|b|c/ Any one of several patterns
13// /\d/ Any digit character
14// /\w/ An alphanumeric character (“word character”)
15// /\s/ Any whitespace character
16// /./ Any character except newlines
17// /\b/ A word boundary
18// /^/ Start of input
19// /$/ End of input
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
1/findme/
2Characters \, ., \cX, \d, \D, \f, \n, \r, \s, \S, \t, \v, \w, \W, \0, \xhh, \uhhhh, \uhhhhh, [\b]
3Assertions ^, $, x(?=y), x(?!y), (?<=y)x, (?<!y)x, \b, \B
4Groups (x), (?:x), (?<Name>x), x|y, [xyz], [^xyz], \Number
5Quantifiers *, +, ?, x{n}, x{n,}, x{n,m}
6Unicode \p{UnicodeProperty}, \P{UnicodeProperty}
7javascript
8let re = /findme/
9let defaults = new RegExp('compiled');
10defaults = { dotAll: false, flags: "", global: false, ignoreCase: false, falselastIndex: 0, multiline: false, source: "abc", sticky: false, unicode: false}
1Parameters:
2regex - a delimiting regular expression
3
4
5Returns:
6An array of strings computed by splitting the given string.
7
8
9Throws:
10PatternSyntaxException - if the provided regular expression’s
11 syntax is invalid.
12