我对正则表达式还很陌生,所以我确信我遗漏了一些明显的东西,但我需要帮助解决以下问题
我想从特定的子字符串中提取后面的字符串。我正在整理一个扫描文档列表,下面是示例字符串,我想提取“FORENAME”之后的所有内容
这就是我到目前为止所做的:
regex = r"(?<=(FORE))[A-Z]+"
test_str = 'UNIQUE NUMBER 12345 678910 11 FROM THIS DOCUMENT | . ISSUED ON 2011-04-04 FORENAME GUIDO \\ SURNAME VAN ROSSUM. '
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=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)))
返回以下内容:
Match 1 was found at 78-82: NAME
Group 1 found at 74-78: FORE
我希望它返回的是:
GUIDO \ SURNAME VAN ROSSUM.
谢谢
这么简单的问题不需要正则表达式
基于上述内容,您可以使用:
Demo
相关问题 更多 >
编程相关推荐