简体   繁体   English

Python 正则表达式:查找给定模式匹配的所有跨度(匹配、开始、结束)

[英]Python regex: find ALL spans (match, start, end) of a given pattern matched

I need to build a list of all matches of a regex in my text along with their start and end positions in it.我需要在我的文本中构建一个正则表达式的所有匹配项以及它们在其中的开始和结束位置的列表。

While the re.search returns a match object for which I can leverage the match.start and match.end properties, the re.findall seems to only return the list of matched strings but no match objects.虽然 re.search 返回一个匹配 object 我可以利用 match.start 和 match.end 属性,但 re.findall 似乎只返回匹配字符串的列表,但没有匹配对象。

Here is some code to demonstrate:下面是一些代码来演示:

import re

text = "40 boxed and 25 crates weight 254 pounds and occupy 23 cubic feet."
searchfor = r"(\d+)"

match = re.search(searchfor, text)
if match:
    print(f"Pattern {match.group()} found at {match.start()} until {match.end()}")

matches = re.findall(searchfor, text)

for match in matches:
    # print(f"Pattern {match.group()} found at {match.start()} until {match.end()}")
    print(match)

Thank you for any insight.感谢您的任何见解。 Happy 2022 2022年快乐

re.finditer() is exactly what you're looking for: re.finditer()正是您正在寻找的:

matches = re.finditer(searchfor, text)

for match in matches:
    print(f"Pattern {match.group()} found at {match.start()} until {match.end()}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM