regular expressions Flashcards
module for regular expressions
re
?
match 0 or 1 repetitions of the preceding RE
make qualifiers not greedy
*?, +?, ??
match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible
{m,n}?
set of characters
[amk] [a-z] [0-9A-Fa-f] [^5]
named group
(?P…)
A backreference to a named group
(?P=name)
lookahead assertion
(?=…)
negative lookahead assertio
(?!…)
positive lookbehind assertion
(?<=…)
negative lookbehind assertion
(?
yes/no pattern
(?(id/name)yes-pattern|no-pattern)
Matches the contents of the group of the same number
\number
(.+) \1 matches ‘the the’ or ‘55 55’
Matches the empty string, but only at the beginning or end of a wor
\b
r’\bfoo\b’ matches ‘foo’, ‘foo.’, ‘(foo)’, ‘bar foo baz’ but not ‘foobar’ or ‘foo3’.
ompile a regular expression pattern into a regular expression objec
re.compile(pattern, flags=0)
Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object.
re.search(pattern, string, flags=0)
Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
Split string by the occurrences of pattern.
re.split(pattern, string, maxsplit=0, flags=0)
Return all non-overlapping matches of pattern in string, as a list of strings
re.findall(pattern, string, flags=0)
Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string.
re.finditer(pattern, string, flags=0)
get entire found string from match
m.group(0), m[0]
Return the indices of the start and end of the substring matched by group
Match.start([group])
Match.end([group])