我有一个多行字符串,我需要太阳字符串...?这是我放置的输入字符串和必需的输出字符串

时间:2019-06-06 04:57:03

标签: python

输入字符串:-

r="""Jun 5 13:09:36.132: %ASESDK-5-NOTICE: dcmd[6670] 879 libloadindexmapmanager.ResilienceStatusEventHandler: uid=[1804108988984000615]userplaneFullyResilient:        User plane resilience alarm cleared in 899 seconds, affected users 0 (1)...
   Jun 5 13:09:36.132: %ASESDK-5-NOTICE: dcmd[6670] 879 libloadindexmapmanager.ResilienceStatusEventHandler: .333252 (2)"""

必需的输出:-

r="""Jun 5 13:09:36.132: %ASESDK-5-NOTICE: dcmd[6670] 879 libloadindexmapmanager.ResilienceStatusEventHandler: uid=[1804108988984000615]userplaneFullyResilient:        User plane resilience alarm cleared in 899 seconds, affected users 0.333252 """

1 个答案:

答案 0 :(得分:0)

我认为您想做这样的事情:

import re

regex = r"(.*) \(1\)(.*): (.*) "

new_string = ""

test_str = "Jun 5 13:09:36.132: %ASESDK-5-NOTICE: dcmd[6670] 879 libloadindexmapmanager.ResilienceStatusEventHandler: uid=[1804108988984000615]userplaneFullyResilient: User plane resilience alarm cleared in 899 seconds, affected users 0 (1)... Jun 5 13:09:36.132: %ASESDK-5-NOTICE: dcmd[6670] 879 libloadindexmapmanager.ResilienceStatusEventHandler: .333252 (2)"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    new_string = (match.group(1) + match.group(3))

print (new_string)

输出为:

Jun 5 13:09:36.132: %ASESDK-5-NOTICE: dcmd[6670] 879 libloadindexmapmanager.ResilienceStatusEventHandler: uid=[1804108988984000615]userplaneFullyResilient: User plane resilience alarm cleared in 899 seconds, affected users 0.333252