python re note[part1]

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 either A or B, 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值