我有一个小的正则表达式要处理。我有 2 个不同的术语。
- “美国”,我想忽略大小写匹配
- “US”,我想匹配而不忽略大小写。
我想在单个正则表达式替换语句中执行以下两个正则表达式替换。
clntxt = re.sub('(?i)United States', 'USA', "united states")
# Output: USA
clntxt = re.sub('US', 'USA', "US and us")
# output: USA and us
我需要类似的东西
clntxt = re.sub('(?i)United States|(?s)US', 'USA', "united states and US and us")
# output: USA and USA and us
如何实现上述目标?
请您参考如下方法:
在旧版 Python 中,(?i) 为整个表达式 打开“忽略大小写”标志。来自官方文档:
(?aiLmsux)
(One or more letters from the set 'a', 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function. Flags should be used first in the expression string.
然而,从 Python 3.6 开始,您可以在表达式的一部分内切换标志:
(?imsx-imsx:...)
(Zero or more letters from the set 'i', 'm', 's', 'x', optionally followed by '-' followed by one or more letters from the same set.) The letters set or removes the corresponding flags: re.I (ignore case), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the part of the expression. (The flags are described in Module Contents.)
New in version 3.6.
例如,(?i:foo)bar 匹配 foobar 和 FOObar 但不匹配 fooBAR。所以回答你的问题:
>>> re.sub('(?i:United States)|US', 'USA', 'united states and US and us')
'USA and USA and us'
请注意,这仅适用于 Python 3.6+。
