【问题标题】:Simple case folding vs full case folding in Python regex modulePython regex 模块中的简单大小写折叠与完整大小写折叠
【发布时间】:2019-07-03 08:33:08
【问题描述】:

这是我要询问的模块:https://pypi.org/project/regex/,它是 Matthew Barnett 的 regex

在项目描述页面中,V0 和 V1 之间的行为差​​异表示为(注意粗体部分):

旧行为与新行为

为了兼容re模块,这个模块有2个 行为:

  • 版本 0 行为(旧行为,与 re 模块兼容):

    请注意,re 模块的行为可能会随着时间而改变,并且 我会努力在版本 0 中匹配这种行为。

    • 由模式中的VERSION0V0 标志或(?V0) 指示。
    • Unicode 中不区分大小写的匹配使用 简单的大小写折叠 默认。
  • 版本 1 行为(新行为,可能不同于 re 模块):

    • 由模式中的VERSION1V1 标志或(?V1) 指示。
    • Unicode 中不区分大小写的匹配默认使用完全大小写折叠

如果没有指定版本,正则表达式模块将默认为regex.DEFAULT_VERSION

我自己尝试了几个例子,但没有弄清楚它的作用:

Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import regex
>>> r = regex.compile("(?V0i)и")
>>> r
regex.Regex('(?V0i)и', flags=regex.I | regex.V0)
>>> r.search("И")
<regex.Match object; span=(0, 1), match='И'>
>>> regex.search("(?V0i)é", "É")
<regex.Match object; span=(0, 1), match='É'>
>>> regex.search("(?V0i)é", "E")
>>> regex.search("(?V1i)é", "E")

简单案例折叠和全案例折叠有什么区别?或者您能否提供一个示例,其中(不区分大小写)正则表达式匹配 V1 中的某些内容但 V0 中不匹配?

【问题讨论】:

  • 未测试,但它可能遵循this table。全大小写折叠可以用两个字符替换一些特殊字符,简单的大小写折叠不会。这样的字符是例如大写和小拉丁升号。
  • @MichaelButscher 太好了,它有效。如果你把它写成答案,你会得到一个绿色的勾号。

标签: python regex python-regex


【解决方案1】:

它遵循Unicode case folding table。摘录:

# The entries in this file are in the following machine-readable format:
#
# <code>; <status>; <mapping>; # <name>
#
# The status field is:
# C: common case folding, common mappings shared by both simple and full mappings.
# F: full case folding, mappings that cause strings to grow in length. Multiple characters are separated by spaces.
# S: simple case folding, mappings to single characters where different from F.

[...]

# Usage:
#  A. To do a simple case folding, use the mappings with status C + S.
#  B. To do a full case folding, use the mappings with status C + F.

折叠仅对少数特殊字符有所不同,例如小和大写的拉丁尖 s:

00DF; F; 0073 0073; # LATIN SMALL LETTER SHARP S

[...]

1E9E; F; 0073 0073; # LATIN CAPITAL LETTER SHARP S
1E9E; S; 00DF; # LATIN CAPITAL LETTER SHARP S

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2018-01-03
    • 2011-04-10
    • 1970-01-01
    • 2021-07-03
    • 2019-02-21
    • 1970-01-01
    • 2020-09-13
    相关资源
    最近更新 更多