[英]Regex findall start() and end() ? Python
i'm trying to get the start and end positions of a query in sequence by using re.findall 我正在尝试使用re.findall按顺序获取查询的开始和结束位置
import re
sequence = 'aaabbbaaacccdddeeefff'
query = 'aaa'
findall = re.findall(query,sequence)
>>> ['aaa','aaa']
how do i get something like findall.start() or findall.end() ? 我如何得到像findall.start()或findall.end()的东西?
i would like to get 我想得到
start = [0,6]
end = [2,8]
i know that 我知道
search = re.search(query,sequence)
print search.start(),search.end()
>>> 0,2
would give me only the first instance 只会给我第一个例子
Use re.finditer
: 使用
re.finditer
:
>>> import re
>>> sequence = 'aaabbbaaacccdddeeefff'
>>> query = 'aaa'
>>> r = re.compile(query)
>>> [[m.start(),m.end()] for m in r.finditer(sequence)]
[[0, 3], [6, 9]]
From the docs: 来自文档:
Return an
iterator
yieldingMatchObject
instances over all non-overlapping matches for the RE pattern in string.返回一个
iterator
在字符串中的RE模式的所有非重叠匹配上产生MatchObject
实例。 The string is scanned left-to-right, and matches are returned in the order found.从左到右扫描字符串,并按找到的顺序返回匹配项。
You can't. 你不能。
findall
is a convenience function that, as the docs say, returns "a list of strings". findall
是一个便利函数,正如文档所说,返回“字符串列表”。 If you want a list of MatchObject
s, you can't use findall
. 如果需要
MatchObject
列表,则不能使用findall
。
However, you can use finditer
. 但是,您可以使用
finditer
。 If you're just iterating over the matches for match in re.findall(…):
, you can use for match in re.finditer(…)
the same way—except you get MatchObject
values instead of strings. 如果你只是
for match in re.findall(…):
迭代匹配for match in re.findall(…):
,你可以用for match in re.finditer(…)
的for match in re.finditer(…)
方式相同 - 除了你获得MatchObject
值而不是字符串。 If you actually need a list, just use matches = list(re.finditer(…))
. 如果你确实需要一个列表,只需使用
matches = list(re.finditer(…))
。
Use finditer instead of findall. 使用finditer而不是findall。 This gives you back an iterator yielding MatchObject instances and you can get start/end from the MatchObject.
这会让你返回一个产生MatchObject实例的迭代器,你可以从MatchObject获得开始/结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.