简体   繁体   English

正则表达式查找字符串之间的所有模式

[英]Regex find ALL patterns between string

I want to match digits betwen "000" or betwen \\b and "000" or "000" and \\b from a string like this: 我想从像这样的字符串中匹配“ 000”或“ \\ b”和“ 000”或“ 000”和\\ b之间的数字:

11101110001011101000000011101010111

I have tried with expressions like this: 我试过这样的表达式:

(?<=000)\d+(?=000)

but I only get the largest occurrence 但是我最多

I expect to get: 我希望得到:

1110111
1011101
0
11101010111

You can use the regex package and the .findall() method: 您可以使用regex.findall()方法:

In [1]: s = "11101110001011101000000011101010111"

In [2]: import regex

In [3]: regex.findall(r"(?<=000|^)\d+?(?=000|$)", s)
Out[3]: ['1110111', '1011101', '0', '00011101010111']

The 000|^ and 000|$ would help to match either the 000 or the beginning and the end of a string respectively. 000|^000|$有助于匹配字符串000或字符串的开头和结尾。 Also note the ? 还注意? after the \\d+ - we are making it non-greedy . \\d+ -我们将其设为非贪婪

Note that the regular re.findall() would fail with the following error in this case: 请注意,在这种情况下,常规re.findall()将失败,并显示以下错误:

error: look-behind requires fixed-width pattern 错误:向后看需要固定宽度的图案

This is because re does not support variable-length lookarounds but regex does. 这是因为re不支持变长查找,regex支持。

您可以使用re模块来做到这一点:

re.findall(r'(?:\b|(?<=000))(\d+?)(?:000|\b)', s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM