首页 > 解决方案 > 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

++++++++++++++++++++++++++++++++++++++++++

标签: pythonregex

解决方案


所以首先,你没有很好地格式化你的问题。我尝试为您格式化,但您需要更多文字来解释。

基本上根据我的理解,问题是有一个字符串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 [ 10.0s] 12 0

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


推荐阅读