2017-08-02 162 views
-1

我尝试Action Plan后,一路为新的线路匹配所有文字从字Problem Description的组内的所有文字......这正则表达式是不工作...正则表达式 - 匹配关键字

(?=.*\bProblem Description\b)(?=.*\bBusiness Impact\b)(?=.*\bTroubleshooting\b)(?=.*\bCurrent Status\b)(?=.*\bAction Plan\b).+ 

这是我想要匹配的文本...我想返回所有这些文本...有一种方法可以通过一系列关键字进行匹配吗?

Problem Description: Customer reported an problem with a card that was receiving a "broken chip error message". 

Business Impact: Unknown 

Troubleshooting: Collected the alarm history and the debug logs. 

Current Status: Customer switched slots with several differnt cards and isolated it down to two defective cards. 

Action Plan: once the completed form is returned will issue RMA. 

回答

1

这与您的样品:

(Problem Description:)(.|\s)*(Action Plan:.*) 
0

为了让所有的文字我并不需要回顾后...还需要斑点都包括换行符。

# coding=utf8 
# the above tag defines encoding for this document and is for Python 2.x compatibility 

import re 

regex = r"(Problem Description)(.*.)(Action Plan)(.*.)" 

test_str = ("Problem Description: Customer reported an problem with a card that was receiving a \"broken chip error message\".\n\n" 
    "Business Impact: Unknown\n\n" 
    "Troubleshooting: Collected the alarm history and the debug logs.\n\n" 
    "Current Status: Customer switched slots with several differnt cards and isolated it down to two defective cards. \n\n" 
    "Action Plan: once the completed form is returned will issue RMA.") 

matches = re.finditer(regex, test_str, re.IGNORECASE | re.DOTALL) 

for matchNum, match in enumerate(matches): 
    matchNum = matchNum + 1 

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) 

    for groupNum in range(0, len(match.groups())): 
     groupNum = groupNum + 1 

     print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) 

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.