#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# https://www.v2ex.com/t/619370#; | |
if __name__ == '__main__': | |
# 我现在想通过 110.1.172.32 这个字符串 匹配到 object-group network 后面的 ShangHai 这个关键词, 请问正则怎么写? 感谢各位大佬 | |
text = """ | |
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 | |
""" | |
group = 'object-group' | |
ip = '110.1.172.32' | |
text = text[:text.index(ip)] | |
print "1 找到IP后,忽略后面的文本 \n", text | |
text = text[text.rindex(group):] | |
print "2 从后往前找 object-group, 并忽略前面的文本 \n", text | |
text = text.splitlines()[0] | |
print "3 取第一行内容 \n", text | |
text = text[text.rindex(' ') + 1:] | |
print "4 从后往前找空格,然后忽略空格前面的内容 \n", text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
corningsun commentedNov 15, 2019
运行结果