This repository was archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplexRegexHelp.py
More file actions
29 lines (24 loc) · 1.41 KB
/
complexRegexHelp.py
File metadata and controls
29 lines (24 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# This is simply just a reference file for how to handle complex REGEX
import re
phoneRegex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')
# Let's clean that shit up.
phoneRegex = re.compile(r'''( # The triple quote allows a multi-line string to make the REGEX more legible
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
\d{3} # first 3 digits
(\s|-|\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''', re.VERBOSE) # This VERBOSE argument tells the compiler to IGNORE the whitespace inside the compilefunction
# Additionally, here's a small table with commonly used REGEX character classes
character_classes = {
'\d' : 'Any numeic digit 0-9'
'\D' : 'Any character thats NOT a numeric digit 0-9'
'\w' : 'Any letter, normal digit or the underscore' # Think of me as matching WORD characters.
'\W' : 'Anything thats not a letter, normal digit, or underscore'
'\s' : 'Any space, tab, or newline character' # Think of me as matching "WHITESPACE characters"
'\S' : 'Any character thats NOT a space, tab, or newline character'
'()?' : 'I can match however many times I want. Or none at all'
'()*' : 'I can match ANY amount of times my letters appear' # Bat(wo)?man == Batwowowowowoman
'()+' : 'MUST match more than one time'
}