Python 似乎使用正则表达式错误地识别了区分大小写的字符串
Python seems to incorrectly identify case-sensitive string using regex
我正在使用 Python 2.7 检查区分大小写的字符串模式,它似乎 return 不正确的匹配。我已经 运行 进行了以下测试:
>>> import re
>>> rex_str = "^((BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?.(?i)pdf$)"
>>> not re.match(rex_str, 'BOA_1988-148.pdf')
>>> False
>>> not re.match(rex_str, 'BOA_1988-148.PDF')
>>> False
>>> not re.match(rex_str, 'BOA1988-148.pdf')
>>> True
>>> not re.match(rex_str, 'boa_1988-148.pdf')
>>> False
前三个测试是正确的,但最终测试 'boa_1988-148.pdf' 应该 return 正确,因为该模式应该将前 3 个字符 (BOA) 视为区分大小写。
我用在线测试器 (https://regex101.com/) 检查了表达式,模式是正确的,将最后一个标记为不匹配,因为 'boa' 是小写。我是否遗漏了什么,或者您是否必须使用区分大小写的模式(如 (?c))明确声明一个组区分大小写?
标志不适用于正则表达式的部分。您告诉正则表达式引擎不区分大小写地匹配:
(?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.
强调我的,该标志适用于 整个模式 ,而不仅仅是一个子字符串。如果您只需要匹配 pdf
或 PDF
,请直接在您的模式中使用它:
r"^((BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?.(?:pdf|PDF)$)"
这匹配 .pdf
或 .PDF
。如果您需要匹配大小写的任意混合,请使用:
r"^((BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?.[pP][dD][fF]$)"
(?i)
不仅适用于其自身或包含它的组。来自 the Python 2 re
documentation:
(?iLmsux)
(One or more letters from the set 'i'
, 'L'
, 'm'
, 's'
, 'u'
, 'x'
.) The group matches the empty string; the letters set the corresponding flags […] for the entire regular expression.
一种选择是手动执行:
r"^(BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?\.[Pp][Dd][Ff]\Z"
另一种是使用单独的区分大小写的检查:
rex_str = r"(?i)^(BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?\.pdf\Z"
match = re.match(rex_str, s) if s.startswith("BOA_") else None
或单独的不区分大小写的:
rex_str = r"^(BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?\..{3}\Z"
match = re.match(rex_str, s) if s.lower().endswith(".pdf") else None
我正在使用 Python 2.7 检查区分大小写的字符串模式,它似乎 return 不正确的匹配。我已经 运行 进行了以下测试:
>>> import re
>>> rex_str = "^((BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?.(?i)pdf$)"
>>> not re.match(rex_str, 'BOA_1988-148.pdf')
>>> False
>>> not re.match(rex_str, 'BOA_1988-148.PDF')
>>> False
>>> not re.match(rex_str, 'BOA1988-148.pdf')
>>> True
>>> not re.match(rex_str, 'boa_1988-148.pdf')
>>> False
前三个测试是正确的,但最终测试 'boa_1988-148.pdf' 应该 return 正确,因为该模式应该将前 3 个字符 (BOA) 视为区分大小写。
我用在线测试器 (https://regex101.com/) 检查了表达式,模式是正确的,将最后一个标记为不匹配,因为 'boa' 是小写。我是否遗漏了什么,或者您是否必须使用区分大小写的模式(如 (?c))明确声明一个组区分大小写?
标志不适用于正则表达式的部分。您告诉正则表达式引擎不区分大小写地匹配:
(?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), andre.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 there.compile()
function. Flags should be used first in the expression string.
强调我的,该标志适用于 整个模式 ,而不仅仅是一个子字符串。如果您只需要匹配 pdf
或 PDF
,请直接在您的模式中使用它:
r"^((BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?.(?:pdf|PDF)$)"
这匹配 .pdf
或 .PDF
。如果您需要匹配大小写的任意混合,请使用:
r"^((BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?.[pP][dD][fF]$)"
(?i)
不仅适用于其自身或包含它的组。来自 the Python 2 re
documentation:
(?iLmsux)
(One or more letters from the set
'i'
,'L'
,'m'
,'s'
,'u'
,'x'
.) The group matches the empty string; the letters set the corresponding flags […] for the entire regular expression.
一种选择是手动执行:
r"^(BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?\.[Pp][Dd][Ff]\Z"
另一种是使用单独的区分大小写的检查:
rex_str = r"(?i)^(BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?\.pdf\Z"
match = re.match(rex_str, s) if s.startswith("BOA_") else None
或单独的不区分大小写的:
rex_str = r"^(BOA_[0-9]{4}-[0-9]{1,3})(?:CO)?\..{3}\Z"
match = re.match(rex_str, s) if s.lower().endswith(".pdf") else None