[英]Regex find greedy and lazy matches and all in-between
我有一个像这样的序列'01 02 09 02 09 02 03 05 09 08 09 '
,我想找到一个以01
开头并以09
结尾的序列,并且中间可以有 1 到 9 个两位数,例如02
, 03
, 04
等。这是我到目前为止尝试过的。
我正在使用w{2}\s
( w{2}
用于匹配两个数字,而\s
用于空白)。 这可能会发生一到九次,从而导致(\w{2}\s){1,9}
。 整个正则表达式变为(01\s(\w{2}\s){1,9}09\s)
。 这将返回以下结果:
<regex.Match object; span=(0, 33), match='01 02 09 02 09 02 03 05 09 08 09 '>
如果我使用惰性量词?
,它返回以下结果:
<regex.Match object; span=(0, 9), match='01 02 09 '>
我怎样才能获得中间的结果。 期望的结果将包括以下所有内容:
<regex.Match object; span=(0, 9), match='01 02 09 '>
<regex.Match object; span=(0, 15), match='01 02 09 02 09 '>
<regex.Match object; span=(0, 27), match='01 02 09 02 09 02 03 05 09 '>
<regex.Match object; span=(0, 33), match='01 02 09 02 09 02 03 05 09 08 09 '>
您可以使用提取这些字符串
import re
s = "01 02 09 02 09 02 03 05 09 08 09 "
m = re.search(r'01(?:\s\w{2})+\s09', s)
if m:
print( [x[::-1] for x in re.findall(r'(?=\b(90.*?10$))', m.group()[::-1])] )
# => ['01 02 09 02 09 02 03 05 09 08 09', '01 02 09 02 09 02 03 05 09', '01 02 09 02 09', '01 02 09']
请参阅Python 演示。
使用01(?:\s\w{2})+\s09
模式和re.search
,您可以提取从01
到最后一个09
的子字符串(中间有任何空格分隔两个单词字符块)。
第二步—— [x[::-1] for x in re.findall(r'(?=\b(90.*?10$))', m.group()[::-1])]
- 是将字符串和模式反转得到从09
到01
的所有重叠匹配,然后反转它们得到最终的字符串。
如果在列表理解的末尾添加[::-1]
,也可以反转最终列表: print( [x[::-1] for x in re.findall(r'(?=\b(90.*?10$))', m.group()[::-1])][::-1] )
。
这将是一个非正则表达式的答案,它对匹配元素进行后处理:
s = '01 02 09 02 09 02 03 05 09 08 09 '.trim().split()
assert s[0] == '01' \
and s[-1] == '09' \
and (3 <= len(s) <= 11) \
and len(s) == len([elem for elem in s if len(elem) == 2 and elem.isdigit() and elem[0] == '0'])
[s[:i+1] for i in sorted({s.index('09', i) for i in range(2,len(s))})]
# [
# ['01', '02', '09'],
# ['01', '02', '09', '02', '09'],
# ['01', '02', '09', '02', '09', '02', '03', '05', '09'],
# ['01', '02', '09', '02', '09', '02', '03', '05', '09', '08', '09']
# ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.