gpt4 book ai didi

python正则表达式匹配行在字符串后面包含数字,末尾有数字

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:07 26 4
gpt4 key购买 nike

我使用正则表达式捕获文件中的文本,但该字符串包含错误的数字。我在没有它的情况下捕获,但是当 try catch 下一行时,它仅返回字符串而不返回下一行。当没有错误的尾随数字时,我能够捕获它。

我尝试了许多正则表达式的组合,但尚未成功。

文字:

sentences
company_name: company, ltd6

numbers 99 and letters 99 (I want to match anything here and nothing after)
numbers 99 and letters 99 (I don't want to match anything here or after)

成功捕获正则表达式但带有数字的代码:

company_name = re.findall(r"company_name:\s(.*)\D.+", text)

成功捕获不带数字的正则表达式的代码:

company_name = re.findall(r"company_name:\s(.*)(?=.\D.+)", text)

try catch 以下行:

next_line = re.findall(r"company_name:\s(.*)(?=.\D.+).*", text)

我希望捕获下一行,但没有捕获。

最佳答案

根据你原来的表达,我猜可能是这个表达,

.*company_name:\s*(.*\D)\s*(\w.*)

可能有用。我们有两个组,(.*\D)(\w.*),在其中捕获我们所需的输出。

Demo 1

或者也许是这个:

.*company_name:\s*(.*)\s*(\w.*)

Demo 2

测试

import re

regex = r".*company_name:\s*(.*\D)\s*(\w.*)"

test_str = ("sentences\n"
"company_name: company, ltd6\n\n"
"numbers 99 and letters 99 (I want to match anything here)")

matches = re.finditer(regex, test_str, re.MULTILINE)

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)))

关于python正则表达式匹配行在字符串后面包含数字,末尾有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56799831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com