相关疑难解决方法(0)

Python正则表达式 - re.search()vs re.findall()

对于学校我应该写一个提取IP地址的Python RE脚本.我正在使用的正则表达式似乎可以使用re.search()但不能使用re.findall().

exp = "(\d{1,3}\.){3}\d{1,3}"
ip = "blah blah 192.168.0.185 blah blah"
match = re.search(exp, ip)
print match.group()
Run Code Online (Sandbox Code Playgroud)

对此的匹配总是192.168.0.185,但它与我的不同 re.findall()

exp = "(\d{1,3}\.){3}\d{1,3}"
ip = "blah blah 192.168.0.185 blah blah"
matches = re.findall(exp, ip)
print matches[0]

0.
Run Code Online (Sandbox Code Playgroud)

我想知道为什么re.findall()收益率为0.当re.search()收益率为192.168.0.185时,因为我对两个函数使用相同的表达式.

我能做些什么来实现它re.findall()才能真正遵循正确的表达方式?还是我犯了某种错误?

python regex

20
推荐指数
2
解决办法
3万
查看次数

标签 统计

python ×1

regex ×1