使用Python regex替换部分字符串中出现的所有“pattern”

2024-04-25 22:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用“-”替换苹果和橙色之间的所有“<;”符号。你知道吗

>>> 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')

apple < - orange


Tags: lt苹果reapple符号橙色printorange