1 October 2020

Useful Tool The Regex 101 Site is a great tool for testing regular expressions.

Flags

When you use re.compile, there is a second optional argument called a flag. You can combine flags using the bitwise OR (|) operator. For example re.compile("....", re.DOTALL|re.IGNORECASE) will apply the two flags to your matching.

FlagAction
re.DOTALLCauses . to match \n as well as all other characters
re.IGNORECASECauses match to be case-insenstivie
re.MULTLINECauses ^ to work at the beginning of each line in a triple-quoted string

Some Useful Methods on Compiled Regular Expressions

match(str) This returns the first match object in str; if none is found None is returned.

findall(str) This returns a list of matching strings in str.

finditer(str) This returns an iterator that walks through all of the match objects in str.

Getting information out of Match Objects


>>> s = "abcdefghijklnnopqrstuvwxyzABCDEFGHIJKLNNOPQRSTUVWXYZ0123456789abcdABCD0123456789!@#$%^&*()_+"
>>> seeker = re.compile(r"a.")
>>> for m in seeker.finditer(s):
...     print(m)
... 
<re.Match object; span=(0, 2), match='ab'>
<re.Match object; span=(48, 50), match='ab'>
>>> for m in seeker.finditer(s):
...     print(m.group())
... 
ab
ab
>>> for m in seeker.finditer(s):
...     print(m.span())
... 
(0, 2)
(48, 50)
>>> 

Multiplicity Operators

All multiplicity operators are postfix unary operators. They have precedence over juxtaposition. To override the order of operations, use parentheses.

OperatorAction
{n}exactly n times
{n,}at least n
{m, n}at least m but no more than n
+one or more
*zero or more
?once or nonce

Searching Files


with open("someFile.txt", "r") as fp:
    for line in file:
        #do something