【发布时间】:2016-03-27 06:37:40
【问题描述】:
这里是一个例子:'192.168.1.1;192.168.1.2'
我的代码:
import re
regex = '^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[;]?)+$'
r = re.match(regex, '192.168.1.1;192.168.1.2')
r.groups() # => ('192.168.1.2',)
# My expected result => ('192.168.1.1', '192.168.1.2',)
# re.findall(regex, '192.168.1.1;192.168.1.2') => ['192.168.1.2'] is not what I want......
我使用()捕获每个IP,但结果只显示一个IP。
是不是我的用法不对?
感谢您的帮助。
【问题讨论】:
-
使用
findall -
你想要两个 IP 在不同的线路上吗?
-
@Tushar
findall不会在列表中显示这两个 IP。我想也许我的正则表达式是错误的...... -
@stribizhev 我认为问题出在正则表达式上,
findall不会解决问题。