gpt4 book ai didi

python - 在单个语句中处理区分大小写和不区分大小写的正则表达式模式

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:06 25 4
gpt4 key购买 nike

我有一个小的正则表达式要处理。我有 2 个不同的术语。

  1. “美国”,我想忽略大小写匹配
  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 匹配 foobarFOObar 但不匹配 fooBAR。所以回答你的问题:

>>> re.sub('(?i:United States)|US', 'USA', 'united states and US and us')
'USA and USA and us'

请注意,这仅适用于 Python 3.6+。

关于python - 在单个语句中处理区分大小写和不区分大小写的正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37317172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com