lfc990426 发表于 2022-2-28 18:49:09

正则表达式中的findall

import re
pattern = r'(\.{1,3}){3}'
str1 = '127.0.0.1 192.168.1.66'
match = re.findall(pattern,str1)
print(match)

为什么这串代码输出的为
['.1', '.66']


不应该是[''.0.0.1','.168.1.66'] 吗求大佬解释下{:9_221:} {:9_221:}

isdkz 发表于 2022-2-28 18:57:31

findall 只会获取分组里面的内容:
import re
pattern = r'((\.{1,3}){3})'
str1 = '127.0.0.1 192.168.1.66'
match = re.findall(pattern,str1)
print(match)

1365156784 发表于 2022-2-28 21:37:57

import re
pattern = r'(?:\.{1,3}){3}'
str1 = '127.0.0.1 192.168.1.66'
match = re.findall(pattern,str1)
print(match)

页: [1]
查看完整版本: 正则表达式中的findall