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.
Flag | Action |
---|---|
re.DOTALL | Causes . to match \n as well as all other characters |
re.IGNORECASE | Causes match to be case-insenstivie |
re.MULTLINE | Causes ^ 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.
Operator | Action |
---|---|
{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