python raw string notation to suppress backslash:
r"\n"
is a two character string.
if A
and B
are both regex, AB
is regex.
.
: any character except a newline.
^
: start of string.
*
: 0 or more repetitions.
+
: 1 or more repetitions.
?
: 0 or 1 repetitions.
*, +, ?
characters are both greedy, <.*>
matches <a>b<c>
.
How to suppress? use <.*?>
instead.
{m}
:
m
copies.
{m,n}
: from m
to n
copies, greedy.
{m,n}?
: not greedy.
[]
: indicate a set of characters.
-
means range,[0-9a-fA-F]
represents any hex digit.
special characters are suppressed like.,*,?
if^
is the first character inside bracket, means matching by complementing the set.
A|B
: means match eitherA
orB
, it is short-circuit.
(regex)
: capturing group, Parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex.
capture means you are not only interested in just whether the string is matched or not, but also want to use the matched string part later on.
(:?regex)
: non-capturing group.
>>> help(re.findall)
Help on function findall in module re:
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.
>>> x = 'one two three four five six'
>>> reg = '(one)|(two)|(three)|(four)|(five)|(six)'
>>> re.findall(reg, x)
[('one', '', '', '', '', ''), ('', 'two', '', '', '', ''),
('', '', 'three', '', '', ''), ('', '', '', 'four', '', ''),
('', '', '', '', 'five', ''), ('', '', '', '', '', 'six')]
There are 6 patterns, thus 6 tuples. There are 6 groups, thus each tuple has 6 items.
>>> print(re.findall(r"\b((h|s)(ef|(cd|(a|b))))\b","sef scd sa sb hef hcd ha hb"))
[('sef', 's', 'ef', '', ''), ('scd', 's', 'cd', 'cd', ''),
('sa', 's', 'a', 'a', 'a'), ('sb', 's', 'b', 'b', 'b'),
('hef', 'h', 'ef', '', ''), ('hcd', 'h', 'cd', 'cd', ''),
('ha', 'h', 'a', 'a', 'a'), ('hb', 'h', 'b', 'b', 'b')]
There are 8
regex patterns and 5
groups, thus 8 5-elements tuples. Each element represents the captured match for that grouped sub-pattern.
>>> print(re.findall(r"\b(?:(?:h|s)(?:ef|(?:cd|(?:a|b))))\b","sef scd sa sb hef hcd ha hb"))
['sef', 'scd', 'sa', 'sb', 'hef', 'hcd', 'ha', 'hb']
Please use https://pythex.org/ for the any testing of regular expressions used by Python.
Reference:
https://docs.python.org/2/library/re.html
https://www.zhihu.com/question/29764272