from itertools import combinations
line= "abbb"
for x in range(1, len(line)+1):
print "".join(list(combinations(line, x))[0])
输出
^{pr2}$
请注意,这是一个特殊情况,对于更复杂的模式,它可能不是一个好的解决方案!在
使用正则表达式
import re
text= "abbb"
pattern = re.compile('ab*')
e = 1
while True and e < len(text)+1:
match = pattern.search(text, 0, e)
if not match:
break
s = match.start()
print text[s:e]
e += 1
Regex不会帮助您实现您想要的。 对于像
ab*
这样的模式和abbbbb
这样的行,您可以这样做:输出
^{pr2}$请注意,这是一个特殊情况,对于更复杂的模式,它可能不是一个好的解决方案!在
使用正则表达式
输出
^{pr2}$另一种正则表达式方法,它反转前面的字符串,并使用包含在lookahead中的反向模式来获得重叠匹配:
相关问题 更多 >
编程相关推荐