我试图将Problem Description
中的所有文字一直匹配到Action Plan
之后的新行...此RegEx无效......
(?=.*\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.
答案 0 :(得分:1)
这适用于您的样本:
(Problem Description:)(.|\s)*(Action Plan:.*)
答案 1 :(得分:0)
为了得到所有文字,我不需要lookbehind ...还需要点全部包含换行符。
# 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.