[英]How can I get a list of all possible matches in between non-greedy and greedy
I have the string "I like lettuce and carrots and onions"
in Python. 我在Python中有字符串
"I like lettuce and carrots and onions"
。
I thought I could get the following matches ["I like lettuce", "I like lettuce and carrots", "I like lettuce and carrots and onions"]
by using a regex like .* and
. 我以为我可以通过使用
.* and
。等正则表达式来获得以下匹配项: ["I like lettuce", "I like lettuce and carrots", "I like lettuce and carrots and onions"]
。 (The regex should match any character up to " and".) (正则表达式应匹配“ and”之前的任何字符。)
However, using the greedy version ( .* and
) gives me only the last match, and using the non-greedy version ( .*? and
) gives me only the first match. 但是,使用贪婪版本(
.* and
)仅给我最后一个匹配项,而使用非贪婪版本( .*? and
)仅给我最后一个匹配项。
How can I get all three matches? 如何获得全部三场比赛?
(I do not need a regex solution.) (我不需要正则表达式解决方案。)
For fun, use the string partition
method in Python 3. It searches a string for a substring, and returns a 3-tuple. 为了娱乐起见,请使用Python 3中的字符串
partition
方法。它将在字符串中搜索子字符串,并返回3元组。 When there's a match, it's 有比赛的时候
(string before the match, the match, string after the match)
(比赛之前的字符串,比赛,比赛之后的字符串)
Once you're used to it, it's very pleasant - no indexing needed, and it makes it easy to get the right results. 一旦习惯了,它会非常令人愉悦-无需索引,并且很容易获得正确的结果。 So while this code is longer than some other ways, you should be able to reason about it easily:
因此,尽管此代码比其他方法更长,但您应该能够轻松地对此进行推理:
def findallprefix(s, sep):
sofar = ""
while True:
head, matched, s = s.partition(sep)
if matched:
assert matched == sep
sofar += head
yield sofar
sofar += matched
else:
break
s = "I like lettuce and carrots and onions and dressing."
for match in findallprefix(s, " and"):
print(repr(match))
which prints 哪个打印
'I like lettuce'
'I like lettuce and carrots'
'I like lettuce and carrots and onions'
I wouldn't use an re at all: What is wrong with: 我根本不会使用re:怎么了?
p = "I like lettuce and carrots and onions and dressing.".split("and")
which gives you a list from which you construct the desired strings. 这会为您提供一个列表,您可以从中构建所需的字符串。
You can use simple splitting and construct strings without an expensive regex
: 您可以使用简单的拆分和构造字符串而无需使用昂贵的
regex
:
s = "I like lettuce and carrots and onions and dressing."
splitted = s.split('and')
for x in range(1, len(splitted)):
print('and'.join(splitted[:x]))
# I like lettuce
# I like lettuce and carrots
# I like lettuce and carrots and onions
If you need result in a list, go for a list-comprehension: 如果您需要列表结果,请进行列表理解:
>>> s = "I like lettuce and carrots and onions and dressing."
>>> splitted = s.split('and')
>>> ['and'.join(splitted[:x]) for x in range(1, len(splitted))]
['I like lettuce ', 'I like lettuce and carrots ', 'I like lettuce and carrots and onions ']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.