RegEx in Python

Flags

- - -
re.I re.IGNORECASE Ignore case
re.M re.MULTILINE Multiline
re.L re.LOCALE Make \w,\b,\s locale dependent
re.S re.DOTALL Dot matches all (including newline)
re.U re.UNICODE Make \w,\b,\d,\s unicode dependent
re.X re.VERBOSE Readable style

Functions

Function Description
re.findall Returns a list containing all matches
re.finditer Return an iterable of match objects (one for each match)
re.search Returns a Match object if there is a match anywhere in the string
re.split Returns a list where the string has been split at each match
re.sub Replaces one or many matches with a string
re.compile Compile a regular expression pattern for later use
re.escape Return string with all non-alphanumerics backslashed

Examples

#re.search()

>>> sentence = 'This is a sample string'
>>> bool(re.search(r'this', sentence, flags=re.I))
True
>>> bool(re.search(r'xyz', sentence))
False

#re.findall()

>>> re.findall(r'\bs?pare?\b', 'par spar apparent spare part pare')
['par', 'spar', 'spare', 'pare']
>>> re.findall(r'\b0\*[1-9]\d{2,}\b', '0501 035 154 12 26 98234')
['0501', '154', '98234']

#re.finditer()

>>> m_iter = re.finditer(r'[0-9]+', '45 349 651 593 4 204')
>>> [m[0] for m in m_iter if int(m[0]) < 350]
['45', '349', '4', '204']

#re.split()

>>> re.split(r'\d+', 'Sample123string42with777numbers')
['Sample', 'string', 'with', 'numbers']

#re.sub()

>>> ip_lines = "catapults\nconcatenate\ncat"
>>> print(re.sub(r'^', r'\* ', ip_lines, flags=re.M))
* catapults
* concatenate
* cat

#re.compile()

>>> pet = re.compile(r'dog')
>>> type(pet)
<class '\_sre.SRE\_Pattern'>
>>> bool(pet.search('They bought a dog'))
True
>>> bool(pet.search('A cat crossed their path'))
False
Comments