寻找正则表达式以匹配以下内容

时间:2018-11-15 11:11:30

标签: python regex

string = '***All Done***, ***Execution Success 5664 for $**$ files***'

re pattern = ([\w+].*[\$\*\*]\$.*[\w+]|[\w+])

output i'm getting: 'All Done***, ***Execution Success for $**$ files'

预期输出:All Done Execution Success 5664 for $**$ files

应仅获取字母数字字符和$\*\*$。其余所有应清除。任何帮助,将不胜感激。如果在字符串上找不到$\*\*$,则它应返回:All Done Execution Success 5664 for files

1 个答案:

答案 0 :(得分:0)

这似乎适用于您提供的示例(Python 3.6):

import re

strings = [
    '***All Done***, ***Execution Success 5664 for $**$ files***',
    'Error not ** found. Restore $**$ again **'
]

pattern = "\w+|\$\*\*\$"
cpattern = re.compile(pattern)

for string in strings:
    print(" ".join(cpattern.findall(string)))

输出:

 All Done Execution Success 5664 for $**$ files
 Error not found Restore $**$ again
相关问题