我怎样才能得到非贪婪和贪婪之间所有可能匹配的列表
How can I get a list of all possible matches in between non-greedy and greedy
我在 Python 中有字符串 "I like lettuce and carrots and onions"
。
我想我可以通过使用像 .* and
这样的正则表达式来获得以下匹配项 ["I like lettuce", "I like lettuce and carrots", "I like lettuce and carrots and onions"]
。 (正则表达式应匹配“和”之前的任何字符。)
然而,使用贪婪版本 (.* and
) 只给我最后一场比赛,而使用非贪婪版本 (.*? and
) 只给我第一场比赛。
我怎样才能获得全部三场比赛?
(我不需要正则表达式解决方案。)
我根本不会使用 re: 有什么问题:
p = "I like lettuce and carrots and onions and dressing.".split("and")
这会为您提供一个列表,您可以从中构建所需的字符串。
您可以使用简单的拆分和构造字符串,而无需昂贵的 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
如果您需要列表中的结果,请进行列表理解:
>>> 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 ']
为了好玩,使用 Python 中的字符串 partition
方法 3. 它在字符串中搜索子字符串,并在 returns 中搜索三元组。当有匹配时,它是
(string before the match, the match, string after the match)
一旦你习惯了它,它就会非常愉快 - 不需要索引,而且它可以很容易地获得正确的结果。因此,虽然此代码比其他一些方式更长,但您应该能够轻松地推断出它:
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))
打印
'I like lettuce'
'I like lettuce and carrots'
'I like lettuce and carrots and onions'
我在 Python 中有字符串 "I like lettuce and carrots and onions"
。
我想我可以通过使用像 .* and
这样的正则表达式来获得以下匹配项 ["I like lettuce", "I like lettuce and carrots", "I like lettuce and carrots and onions"]
。 (正则表达式应匹配“和”之前的任何字符。)
然而,使用贪婪版本 (.* and
) 只给我最后一场比赛,而使用非贪婪版本 (.*? and
) 只给我第一场比赛。
我怎样才能获得全部三场比赛?
(我不需要正则表达式解决方案。)
我根本不会使用 re: 有什么问题:
p = "I like lettuce and carrots and onions and dressing.".split("and")
这会为您提供一个列表,您可以从中构建所需的字符串。
您可以使用简单的拆分和构造字符串,而无需昂贵的 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
如果您需要列表中的结果,请进行列表理解:
>>> 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 ']
为了好玩,使用 Python 中的字符串 partition
方法 3. 它在字符串中搜索子字符串,并在 returns 中搜索三元组。当有匹配时,它是
(string before the match, the match, string after the match)
一旦你习惯了它,它就会非常愉快 - 不需要索引,而且它可以很容易地获得正确的结果。因此,虽然此代码比其他一些方式更长,但您应该能够轻松地推断出它:
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))
打印
'I like lettuce'
'I like lettuce and carrots'
'I like lettuce and carrots and onions'