import os,re
def test():
list = re.findall(r'(255\.){2}','255.255.252.255.255.12')
print list
if __name__ == '__main__':
test()
the output :“['255.', '255.']”
输出:“(255年。”、“255年。””
why not 【255.255,255.255】 ?
为什么不【255.255,255.255】呢?
the mactch object should is "255.255"
mactch对象应该是“255.255”
How can i get the correct output result?
如何得到正确的输出结果?
2 个解决方案
#1
1
Mm, not quite. First off, you'll want a non-capturing group - the capturing group you have there will only capture '255.', and use that as the output for re.findall
.
嗯,不完全是。首先,您需要一个非捕获组——您拥有的捕获组将只捕获'255。,并将其用作re.findall的输出。
Example:
例子:
re.findall(r'(?:255\.){2}', '255.255.252.255.255.12')
The (?:) construct is a non-capturing group - and without any capturing groups, re.findall returns the entire matching string.
构造(?:)是一个非捕获组——没有任何捕获组,re.findall返回整个匹配的字符串。
Note that this won't actually return ['255.255', '255.255']
- it will actually return ['255.255.', '255.255.']
.
注意,它实际上不会返回['255.255','255.255']-它实际上会返回['255.255']。”、“255.255。”)。
#2
2
In your regex, you're only capturing the first 255.
. You need to wrap everything you want to capture in a capturing group:
在您的regex中,您只捕获了前255..你需要把你想要捕捉的每一件东西都包裹起来:
>>> re.findall(r'((?:255\.){2})','255.255.252.255.255.12')
['255.255.', '255.255.']
(?:...)
is a non-capturing group. It basically lets you group things without having them show up as a captured group.
是一个不抓人的群体。它基本上允许你对事物进行分组,而不让它们作为一个捕获的组显示出来。
#1
1
Mm, not quite. First off, you'll want a non-capturing group - the capturing group you have there will only capture '255.', and use that as the output for re.findall
.
嗯,不完全是。首先,您需要一个非捕获组——您拥有的捕获组将只捕获'255。,并将其用作re.findall的输出。
Example:
例子:
re.findall(r'(?:255\.){2}', '255.255.252.255.255.12')
The (?:) construct is a non-capturing group - and without any capturing groups, re.findall returns the entire matching string.
构造(?:)是一个非捕获组——没有任何捕获组,re.findall返回整个匹配的字符串。
Note that this won't actually return ['255.255', '255.255']
- it will actually return ['255.255.', '255.255.']
.
注意,它实际上不会返回['255.255','255.255']-它实际上会返回['255.255']。”、“255.255。”)。
#2
2
In your regex, you're only capturing the first 255.
. You need to wrap everything you want to capture in a capturing group:
在您的regex中,您只捕获了前255..你需要把你想要捕捉的每一件东西都包裹起来:
>>> re.findall(r'((?:255\.){2})','255.255.252.255.255.12')
['255.255.', '255.255.']
(?:...)
is a non-capturing group. It basically lets you group things without having them show up as a captured group.
是一个不抓人的群体。它基本上允许你对事物进行分组,而不让它们作为一个捕获的组显示出来。