【发布时间】:2016-10-29 12:19:57
【问题描述】:
示例:
>>> line = 'the the, To to'
>>> re.findall(r'\b(\w+) \1', line)
['the']
>>> re.findall(r'\b(\w+) \1', line, re.I)
['the', 'To']
>>> re.sub(r'\b(\w+) \1', r'\1', line, re.I)
'the, To to'
预期:
'the, To'
正则表达式适用于其他地方,例如
- Vim:
s/\v<(\w+) \1/\1/gi - Perl:
s/\b(\w+) \1/$1/gi - sed:
-r 's/\b(\w+) \1/\1/gi'
这是一种已知的行为吗?什么是解决方法?我的 Python 版本是 3.4.3,如果这有影响的话。
【问题讨论】:
-
这不是一个真正的
perl问题,是吗?但核心区别在于 - 您的模式中似乎没有gi修饰符。 -
@Sobrique OP 有
re.I,忽略大小写标志,re.sub默认为“全局”;但是,他们没有正确传递标志。
标签: python regex python-3.x