2017-08-02 160 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.