簡體   English   中英

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

[英]Python: Match string with multiple patterns

我在變量下面有字符串

'''
                                 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')].

我已經在下面的腳本t中使用了輸出。 +++++++++++++++++++++++++++++++++++++++

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

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

因此,首先,您還沒有很好地格式化問題。 我會嘗試為您設置格式,但您需要更多文字來說明。

基本上從我的理解來看,問題是一個字符串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

希望這是您要尋找的內容,請為問題添加更多描述,以便您可以很好地格式化代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM