python - 为什么这个正则表达式只给出第三个八位字节?
问题描述
ip = 'The management ip is 10.0.55.0 and the remote ip is 192.167.13.4.\n1.1.22.1 Random sentence'
pat = re.compile('(\d{1,3}\.){3}')
ips = re.findall(pat,ip)
>>> ips
['55.', '13.', '22.']
我本来希望输出是 ['10.0.55.','192.168.13.','1.1.22.']
解决方案
findall
返回任何捕获匹配组的内容,如果有的话 - 你这样做。将匹配组更改为非捕获:
pat = re.compile('(?:\d{1,3}\.){3}')
推荐阅读
- odata - 检查 XML 视图中的空值的日期字段
- ruby-on-rails - Omniauth 没有捕捉到最初的 get "/auth/:provider" 请求
- ruby-on-rails - 如何根据语言动态生成 Rails 表单
- algorithm - 算法等价
- c# - 为生成的 CANoe 解决方案更改 .dll 的输出文件夹
- omnet++ - 如何延迟在 omnet 上记录统计信息?
- sql - 将除一个之外的所有重复项移动到另一个表
- asp.net-core - asp.net core 2.2 - 必需属性不适用于十进制和整数
- javascript - 这个 jQuery 代码在它现在不起作用之前有效,为什么?
- python - 在 sqlite 中使用列别名计算