Python正则表达式如何忽略模式的一部分而不是整个表达式的大小写? [英] How can Python regex ignore case inside a part of a pattern but not the entire expression?

查看:231
本文介绍了Python正则表达式如何忽略模式的一部分而不是整个表达式的大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个包含 foobar fooBAR FOObar FOOBAR 的字符串,并且我想搜索所有包含不区分大小写的 foo或 FOO的实例 but 小写的 bar。在这种情况下, re.findall 应该返回 ['foobar','FOObar']

Say I have a string containing foobar fooBAR FOObar FOOBAR, and I want to search all instances containing a case insensitive "foo" or "FOO" but a lowercase "bar". In this case, re.findall should return ['foobar', 'FOObar'].

此问题的可接受答案解释说可以在C#中使用(?i)foo(?-i)bar 完成,但是Python引发了无效的表达式错误。

The accepted answer for this question explains that it can be done in C# with (?i)foo(?-i)bar, but Python raises an invalid expression error.

Python regex库是否支持这种功能?

Does the Python regex library support such a feature?

推荐答案

re模块不支持范围标志,但是还有一个替代的regex实现,可以实现:

The re module doesn't support scoped flags, but there's an alternative regex implementation which does:

http://pypi.python.org/pypi/regex

这篇关于Python正则表达式如何忽略模式的一部分而不是整个表达式的大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆