Python:使用多个模式匹配字符串

2024-06-25 23:55:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在变量中有下面的字符串

'''
                                 Messages  Retrans   Timeout   Unexpected-Msg
           INVITE ---------->         30        0         0
              100 <----------         30        0         0         0
              180 <----------         12        0         0         18
              200 <----------         12        0         0         0

              ACK ---------->         12        0
             INFO ---------->         12        0         0
              200 <----------         12        0         0         0
       Pause [         10.0s]         12                            0
              BYE ---------->         12        0
              200 <----------         12        0         0         0

'''

我如何才能得到下面的正则表达式匹配输出与一个模式或分钟模式。你知道吗

[('INVITE', '---------->', '30', '0', '0'), ('100', '<---------- ', '30', '0', '0', '0'), ('180', '<---------- ', '12', '0', '0', '18'), ('200', '<---------- ', '12', '0', '0', '0'), ('ACK', '---------->', '12', '0'), ('INFO', '---------->', '12', '0', '0'), ('200', '<---------- ', '12', '0', '0', '0'), ('BYE', '---------->', '12', '0'), ('200', '<---------- ', '12', '0', '0', '0')].

我使用了下面的脚本来获取输出。 +++++++++++++++++++++++++++++++++++++你知道吗

import re

aa = '''
xmlSFT_Client_mscmlivr MSCML_FULL_AUDIO_Script_CA_disabled.
Warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE = 1024
Resolving remote host '10.214.13.168'... Done.
------------------------------ Scenario Screen -------- [1-9]: Change Screen --
  Call-rate(length)   Port   Total-time  Total-calls  Remote-host
  10.0(0 ms)/1.000s   4063      11.24 s           30  10.214.13.168:5060(UDP)

  Call limit reached (-m 30), 0.000 s period  0 ms scheduler resolution
  0 calls (limit 300)                    Peak was 13 calls, after 1 s
  0 Running, 31 Paused, 0 Woken up
  0 dead call msg (discarded)            0 out-of-call msg (discarded)
  2 open sockets

                                 Messages  Retrans   Timeout   Unexpected-Msg
           INVITE ---------->         30        0         0
              100 <----------         30        0         0         0
              180 <----------         12        0         0         18
              200 <----------         12        0         0         0

              ACK ---------->         12        0
             INFO ---------->         12        0         0
              200 <----------         12        0         0         0
       Pause [         10.0s]         12                            0
              BYE ---------->         12        0
              200 <----------         12        0         0         0

'''

a = []

for i in aa.split('\n'):

    if re.findall(r'^\s+(\w+)\s?(.-+.)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)',i,re.MULTILINE):
            a.append(re.findall(r'^\s+(\w+)\s?(.-+.)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)',i,re.MULTILINE)[0])
    elif re.findall(r'^\s+(\w+)\s?(.-+.)\s+(\w+)\s+(\w+)\s+(\w+)',i,re.MULTILINE) :
            a.append(re.findall(r'^\s+(\w+)\s?(.-+.)\s+(\w+)\s+(\w+)\s+(\w+)',i,re.MULTILINE)[0])
    elif re.findall(r'^\s+(\w+)\s?(.-+.)\s+(\w+)\s+(\w+)',i,re.MULTILINE):
            a.append(re.findall(r'^\s+(\w+)\s?(.-+.)\s+(\w+)\s+(\w+)',i,re.MULTILINE)[0])

print a

+


Tags: reinfotimeoutopeninvitemultilinemessagesbye
1条回答
网友
1楼 · 发布于 2024-06-25 23:55:52

所以首先,你的问题格式不好。我试着给你格式化,但你需要更多的文字来解释。你知道吗

基本上,根据我的理解,问题是有一个字符串a

a = '''
  2 open sockets

                                 Messages  Retrans   Timeout   Unexpected-Msg
           INVITE      >         30        0         0
              100 <              30        0         0         0
              180 <              12        0         0         18
              200 <              12        0         0         0

              ACK      >         12        0
             INFO      >         12        0         0
              200 <              12        0         0         0
       Pause [         10.0s]         12                            0
              BYE      >         12        0
              200 <              12        0         0         0

'''

你想要得到这样的结果:('INVITE', ' >', '30', '0', '0'),

我使用以下正则表达式行来实现:

import re

a = '''
  2 open sockets

                                 Messages  Retrans   Timeout   Unexpected-Msg
           INVITE      >         30        0         0
              100 <              30        0         0         0
              180 <              12        0         0         18
              200 <              12        0         0         0

              ACK      >         12        0
             INFO      >         12        0         0
              200 <              12        0         0         0
       Pause [         10.0s]         12                            0
              BYE      >         12        0
              200 <              12        0         0         0

'''
pattern = re.compile(r'(?P<type>\d+|\w+)\s*(?P<dir>\<?\-+\>?)\s+(?P<messages>\d*)[\n\r\s]*(?P<retrans>\d*)[\n\r\s]*(?P<timeout>\d*)[\n\r\s]*(?P<unexpected_msg>\d*)\n',flags=re.MULTILINE)
result = pattern.finditer(a)
result_list = [m.groupdict() for m in result]

结果输出:

...:for i in result_list:
    print(i)

...:
{'type': 'INVITE', 'dir': '     >', 'messages': '30', 'retrans': '0', 'timeout': '0', 'unexpected_msg': ''}
{'type': '100', 'dir': '<     ', 'messages': '30', 'retrans': '0', 'timeout': '0', 'unexpected_msg': '0'}
{'type': '180', 'dir': '<     ', 'messages': '12', 'retrans': '0', 'timeout': '0', 'unexpected_msg': '18'}
{'type': '200', 'dir': '<     ', 'messages': '12', 'retrans': '0', 'timeout': '0', 'unexpected_msg': '0'}
{'type': 'ACK', 'dir': '     >', 'messages': '12', 'retrans': '0', 'timeout': '', 'unexpected_msg': ''}
{'type': 'INFO', 'dir': '     >', 'messages': '12', 'retrans': '0', 'timeout': '0', 'unexpected_msg': ''}
{'type': '200', 'dir': '<     ', 'messages': '12', 'retrans': '0', 'timeout': '0', 'unexpected_msg': '0'}
{'type': 'BYE', 'dir': '     >', 'messages': '12', 'retrans': '0', 'timeout': '', 'unexpected_msg': ''}
{'type': '200', 'dir': '<     ', 'messages': '12', 'retrans': '0', 'timeout': '0', 'unexpected_msg': '0'}

然后可以迭代result_list。你知道吗

而不是像你在问题中提到的那样使用元组列表,因为我认为你可能想知道缺少了什么值。('ACK', ' >', '12', '0')这样可能不会告诉您缺少哪个2值,因为我在pause行看到只有message值和意外的msg值。 Pause [ 10.0s] 12 0

希望这是你正在寻找的,请添加更多的问题描述,以便您可以很好地格式化您的代码。你知道吗

相关问题 更多 >