正则表达式匹配关键字组中的所有文本

2024-06-08 23:46:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试匹配从单词Problem DescriptionAction Plan后的新行的所有文本…此正则表达式不起作用。。。你知道吗

(?=.*\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.

Tags: andthe文本statuswithactioncustomerdescription
2条回答

为了得到所有的文字我不需要向后看…还需要点所有包括换行符。你知道吗

# 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.

这适用于您的示例:

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

相关问题 更多 >