请教一个正则匹配多行问题的

2019-11-14 08:46:58 +08:00
 TOUJOURSER

文本内容:

 network-object host 109.17.49.131
object-group network KaGuan_12
 network-object host 109.17.26.11
 network-object host 109.17.26.12
object-group network ShangHai
 network-object host 110.1.60.91
 network-object host 110.1.60.92
 network-object host 110.1.172.31
 network-object host 110.1.172.32
 network-object host 110.238.250.57
 network-object host 110.238.250.58
object-group network wangguan_test
 network-object host 111.17.13.32
object-group network wangguan_app_2
 network-object host 111.17.9.54
 network-object host 111.17.9.63

我现在想通过 110.1.172.32 这个字符串 匹配到 object-group network 后面的 ShangHai 这个关键词, 请问正则怎么写? 感谢各位大佬

2976 次点击
所在节点    Python
12 条回复
lululau
2019-11-14 08:52:05 +08:00
正则正则,起码把规则说出来啊,你只举一个特例,那你直接把这个特例写成字符串字面量就可以了,不需要正则啊,再说,你这个说的 110.1.172.32 后面也没有 shanghai 啊
TOUJOURSER
2019-11-14 09:00:25 +08:00
@lululau 不好意思描述的不太清楚, 规则是根据 ip 去获取该 ip 所属于的 group.
我想通过 110.1.172.32 去向上查找以"object-group network "开头的那一行最后的 ShangHai 字符.
araraloren
2019-11-14 09:03:58 +08:00
自己字符串直接查找不行么,如果是在练习正则的话当我没说。。
ClericPy
2019-11-14 09:11:13 +08:00
import re

string = r'''network-object host 109.17.49.131
object-group network KaGuan_12
network-object host 109.17.26.11
network-object host 109.17.26.12
object-group network ShangHai
network-object host 110.1.60.91
network-object host 110.1.60.92
network-object host 110.1.172.31
network-object host 110.1.172.32
network-object host 110.238.250.57
network-object host 110.238.250.58
object-group network wangguan_test
network-object host 111.17.13.32
object-group network wangguan_app_2
network-object host 111.17.9.54
network-object host 111.17.9.63'''
ip = '110.1.172.32'.replace('.', r'\.')
result = re.search(
rf'object-group network (.*?)\n[\s\S]*?network-object host {ip}',
string)
if result:
print(result.group(1))
else:
print('Not found.')


KaGuan_12
TOUJOURSER
2019-11-14 09:16:49 +08:00
@ClericPy 感谢, 你写的这个只可以匹配出 KaGuan_12 , 但是我需要的是 ShangHai, 110.1.172.32 是属于 ShangHai group
todd7zhang
2019-11-14 09:30:24 +08:00
import re
a = ''' network-object host 109.17.49.131
object-group network KaGuan_12
network-object host 109.17.26.11
network-object host 109.17.26.12
object-group network ShangHai
network-object host 110.1.60.91
network-object host 110.1.60.92
network-object host 110.1.172.31
network-object host 110.1.172.32
network-object host 110.238.250.57
network-object host 110.238.250.58
object-group network wangguan_test
network-object host 111.17.13.32
object-group network wangguan_app_2
network-object host 111.17.9.54
network-object host 111.17.9.63
'''

re.findall(r'object-group network (\w+)(?:(?:(?!object-group).)*)(?=network-object host 110\.1\.172\.31)', a, re.DOTALL)
ClericPy
2019-11-14 09:33:15 +08:00
@TOUJOURSER #5 哦对了, 正则没法回溯取非贪婪的, 比较好的办法还是
1. 逐行分析解析成 ip: group 映射再查询
2. 或者是用 group 字段做 split 再查询
ClericPy
2019-11-14 09:34:12 +08:00
6 楼的零宽断言是个好方法, 先匹配非 group 再取 group
TOUJOURSER
2019-11-14 09:38:14 +08:00
@ClericPy @todd7zhang 真的非常感谢, 这个问题困扰了我好久
qingshengwen
2019-11-14 12:07:55 +08:00
试试这个看看
object\-group\snetwork\s(.*?)(?:\s*network\-object\shost\s\d+\.\d+\.\d+\.\d+\s*){0,}(?:\s*network\-object\shost\s110.1.172.32\s*)
https://imgur.com/a/loDyJo7
no1xsyzy
2019-11-14 19:39:32 +08:00
写个 FSM 不行吗?这还正则?(不超过十分钟写完)
https://gist.github.com/no1xsyzy/11ba23cd13f90b3f94227d4838fd7e5a
corningsun
2019-11-15 09:43:43 +08:00
用不上正则

如果只需要一次匹配,只是两次截取字符串的问题,代码示例如下:

https://gist.github.com/corningsun/87aac1ca2bc46e698a4d332a93118bbe

如果需要多次匹配,且数据量不大的话,最好的还是解析所有 IP,做哈希表查找比较好。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/619370

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX