import re
A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing). Blah blah blah.
Again, documentation first
https://docs.python.org/3/library/re.html
as always
1 | import re |
You cannot match a Unicode string with a byte pattern or vice-versa.
The special characters in RE
tl;ra (Too long; Read anyway) or maybe you should read the documentation directly.
‘.’
(Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.
‘^’
(Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.
‘$’
Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline.
‘‘
Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible. ab will match ‘a’, ‘ab’, or ‘a’ followed by any number of ‘b’s.
‘+’
Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match ‘a’ followed by any non-zero number of ‘b’s; it will not match just ‘a’.
‘?’
Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either ‘a’ or ‘ab’.
?, +?, ??
The ‘‘, ‘+’, and ‘?’ qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is matched against b
{m}
Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example, a{6} will match exactly six ‘a’ characters, but not five.
{m,n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible.
{m,n}?
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible.
‘'
Either escapes special characters (permitting you to match characters like ‘*’, ‘?’, and so forth), or signals a special sequence
[]
Used to indicate a set of characters.
‘|’
A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B.
(…)
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group
(?…)
This is an extension notation (a ‘?’ following a ‘(‘ is not meaningful otherwise). The first character after the ‘?’ determines what the meaning and further syntax of the construct is.
(?aiLmsux)
(One or more letters from the set ‘a’, ‘i’, ‘L’, ‘m’, ‘s’, ‘u’, ‘x’.) The group matches the empty string; the letters set the corresponding flags: re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the entire regular expression.
(?:…)
A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.
(?imsx-imsx:…)
(Zero or more letters from the set ‘i’, ‘m’, ‘s’, ‘x’, optionally followed by ‘-‘ followed by one or more letters from the same set.) The letters set or removes the corresponding flags: re.I (ignore case), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the part of the expression. (The flags are described in Module Contents.)
(?P
Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name. If the pattern is (?P[‘“]).*?(?P=quote) (i.e. matching a string quoted with either single or double quotes)
(?P=name)
A backreference to a named group; it matches whatever text was matched by the earlier group named name.
(?#…)
A comment; the contents of the parentheses are simply ignored.
(?=…)
Matches if … matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match ‘Isaac ‘ only if it’s followed by ‘Asimov’.
(?!…)
Matches if … doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match ‘Isaac ‘ only if it’s not followed by ‘Asimov’.
(?<=…)
Matches if the current position in the string is preceded by a match for … that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that abc or a|b are allowed, but a* and a{3,4} are not.
(?<!…)
Matches if the current position in the string is not preceded by a match for …
(?(id/name)yes-pattern|no-pattern)
Will try to match with yes-pattern if the group with given id or name exists, and with no-pattern if it doesn’t. no-pattern is optional and can be omitted. For example, (<)?(\w+@\w+(?:.\w+)+)(?(1)>|$) is a poor email matching pattern, which will match with ‘user@host.com‘ as well as ‘user@host.com‘, but not with ‘<user@host.com‘ nor ‘user@host.com>’.
\number
Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) \1 matches ‘the the’ or ‘55 55’, but not ‘thethe’ (note the space after the group).
\A
Matches only at the start of the string.
\b
Matches the empty string, but only at the beginning or end of a word.
\B
Matches the empty string, but only when it is not at the beginning or end of a word. This means that r’py\B’ matches ‘python’, ‘py3’, ‘py2’, but not ‘py’, ‘py.’, or ‘py!’.
\d
For Unicode (str) patterns:
Matches any Unicode decimal digit (that is, any character in Unicode character category [Nd]). This includes [0-9], and also many other digit characters. If the ASCII flag is used only [0-9] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [0-9] may be a better choice).
For 8-bit (bytes) patterns:
Matches any decimal digit; this is equivalent to [0-9].
\D
Matches any character which is not a Unicode decimal digit. This is the opposite of \d. If the ASCII flag is used this becomes the equivalent of [^0-9] (but the flag affects the entire regular expression, so in such cases using an explicit [^0-9] may be a better choice).
\s
Matches Unicode whitespace characters
\S
Matches any character which is not a Unicode whitespace character.
\w
Matches Unicode word characters
\W
Matches any character which is not a Unicode word character.
\Z
Matches only at the end of the string.
OK, let’s move on to Module Contents
1 | re.compile(pattern, flags=0) |
Compile a regular expression pattern into a regular expression object, which can be used for matching using its match() and search() methods
1 | prog = re.compile(pattern) |
is equivalent to
1 | result = re.match(pattern, string) |
but using re.compile() and saving the resulting regular expression object for reuse is more efficient when the expression will be used several times in a single program.
re.A
re.ASCII
re.DEBUG
re.I
re.IGNORECASE
re.L
re.LOCALE
re.M
re.MULTILINE
re.S
re.DOTALL
re.X
re.VERBOSE
This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when in a character class or when preceded by an unescaped backslash. When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.
This means that the two following regular expression objects that match a decimal number are functionally equal:
1 | a = re.compile(r"""\d + # the integral part |
Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object.
1 | re.match(pattern, string, flags=0) |
If zero or more characters at the beginning (be careful) of string match the regular expression pattern, return a corresponding match object.
1 | re.fullmatch(pattern, string, flags=0) |
If the whole string matches the regular expression pattern, return a corresponding match object.
1 | re.split(pattern, string, maxsplit=0, flags=0) |
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
1 | m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist") |
Regular Expression Examples in standard docs
1 | def displaymatch(match): |
Suppose you are writing a poker program where a player’s hand is represented as a 5-character string with each character representing a card, “a” for ace, “k” for king, “q” for queen, “j” for jack, “t” for 10, and “2” through “9” representing the card with that value.
To see if a given string is a valid hand, one could do the following:
1 | valid = re.compile(r"^[a2-9tjqk]{5}$") |
That last hand, “727ak”, contained a pair, or two of the same valued cards. To match this with a regular expression, one could use backreferences as such:
1 | pair = re.compile(r".*(.).*\1") |
To find out what card the pair consists of, one could use the group() method of the match object in the following manner:
1 | pair.match("717ak").group(1) # '7' |
search() vs. match()
of course you can look up on stackoverflow: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).
1 | text = "He was carefully disguised but captured quickly by police." |
Writing a Tokenizer
1 | import collections |
The tokenizer produces the following output:
1 | Token(typ='IF', value='IF', line=2, column=4) |
I know, I know it’s a headache to read these, try PyMOTW-3 instead if you want
https://pymotw.com/3/re/index.html
Out there is an alternative regular expression module, to replace re, called regex.
https://pypi.python.org/pypi/regex/
And read this:
10 Tips to Get More out of Your Regexes
http://pybit.es/mastering-regex.html