简体   繁体   English

Python 多行字符串查找

[英]Python Multiline String Find

I have an Cisco ASA with a VPN tunnel configured.我有一个配置了 VPN 隧道的 Cisco ASA。 I call the CLI command via API and it returns this multiline string:我通过 API 调用 CLI 命令,它返回这个多行字符串:

\nSession Type: LAN-to-LAN\n\nConnection   : 192.168.1.10\nIndex        : 11701                  IP Addr      : 192.168.1.10\nProtocol     : IKEv2 IPsecOverNatT\nEncryption   : IKEv2: (1)AES256  IPsecOverNatT: (1)AES256\nHashing      : IKEv2: (1)SHA256  IPsecOverNatT: (1)SHA256\nBytes Tx     : 0                      Bytes Rx     : 0\nLogin Time   : 23:14:43 EST Fri Dec 3 2021\nDuration     : 0h:11m:50s\n\n

I can't figure out how to get only the "Bytes Rx" plus the number out beside it.我不知道如何只得到“Bytes Rx”加上它旁边的数字。 I've tried searching it like this, but it returns "Bytes Tx":我试过这样搜索它,但它返回“Bytes Tx”:

import re
regex_parse = re.compile(r'[a-zA-Z]+\s[a-zA-Z][a-zA-Z]\s+:\s[0-9]+')

multilinestring = webhook_api_call()
for item in multilinestring:
    a = regex_parse.search(item)
print(a.group(0))

Output: Output:

Bytes Tx     : 0

I want to only get Bytes Rx and the number out beside it我只想得到 Bytes Rx 和它旁边的数字

Looks like you are trying to parse the result of sh vpn-sessiondb l2l from a Cisco ASA.看起来您正在尝试从 Cisco ASA 解析sh vpn-sessiondb l2l的结果。 The output is pretty standard, so I would skip the regex and do the following: output 非常标准,所以我会跳过正则表达式并执行以下操作:

multilinestring = webhook_api_call()
lines = multilinestring.split("\n")
for l in lines:
    if l.find("Bytes Tx") != -1:
        print("Bytes Rx" + l.partition("Bytes Rx")[2])

Output: Output:

Bytes Rx      : 0

Good luck with your code!祝你的代码好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM