[英]REGEX (python) match or return a string after '?', but in a new line, til the end of that line
[英]python regex match line containing numbers after string with digit at end
我使用正則表達式捕獲文件中的文本,但是字符串包含錯誤的數字。 我沒有捕獲它,但是在嘗試捕獲下一行時,它僅返回字符串,而不返回下一行。 當沒有錯誤的尾隨數字時,我能夠捕獲它。
我已經嘗試過許多正則表達式的組合,但尚未成功。
文本:
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)
嘗試捕獲以下行:
next_line = re.findall(r"company_name:\s(.*)(?=.\D.+).*", text)
我希望捕獲下一行,但不要。
這將僅獲得下一行,而忽略后續行:
next_line = re.sub(r".*company_name:[^\n]+\n*([^\n]+).*", r'\1', text, flags=re.S)
即: numbers 99 and letters 99 (I want to match anything here and nothing after)
根據您的原始表達方式,我猜測可能是這種表達方式,
.*company_name:\s*(.*\D)\s*(\w.*)
可能有用。 我們有兩組(.*\\D)
和(\\w.*)
,其中捕獲了我們想要的輸出。
也許這個:
.*company_name:\s*(.*)\s*(\w.*)
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)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.