2024-04-25 22:25:24 发布
网友
我想用“-”替换苹果和橙色之间的所有“<;”符号。你知道吗
>>> print re.sub(r'(apple.*)<(.*orange)', r'\1-\2', r'apple < < orange')
apple < - orange
>>> print re.sub(r'(apple.*)<(?=.*orange)', r'\g<1>-', r'apple < < orange')
对re.sub的一次调用只处理不重叠的匹配。你知道吗
re.sub
一种方法是暴力:
>>> s = 'apple < < orange' >>> old = None >>> while s != old: ... old = s ... s = re.sub(r'(apple.*)<(.*orange)', r'\1-\2', s) ... >>> print s apple - - orange
对
re.sub
的一次调用只处理不重叠的匹配。你知道吗一种方法是暴力:
相关问题 更多 >
编程相关推荐