ai didi

Python正则表达式编译

转载 作者:行者123 更新时间:2023-11-30 23:41:09 24 4
gpt4 key购买 nike

编写以下行的程序员可能使用名为 regex 的 python 包.

UNIT = regex.compile("(?:{A}(?:'{A})?)++|-+|\S".format(A='\p{Word_Break=ALetter}'))

有人可以帮忙解释一下 A='\p{Word_Break=ALetter}'-+ 的含义吗?

最佳答案

\p{property=value} 运算符匹配 unicode 代码点属性,并记录在您链接到的包索引页面上:

  • Unicode codepoint properties, including scripts and blocks

    \p{property=value}; \P{property=value}; \p{value} ; \P{value}

该条目与代码点具有 Word_Break property 的任何 unicode 字符匹配值为 ALetter(Unicode 代码点数据库中当前有 24941 个匹配项,有关详细信息,请参阅 Unicode Text Segmentation, Word Boundaries chapter specifiation)。

您给出的示例也使用标准 python string formatting将部分表达式插入正在编译的正则表达式中。 “{A}”部分只是要填充的 .format(A='...') 部分的占位符。最终结果是:

"(?:\p{Word_Break=ALetter}(?:'\p{Word_Break=ALetter})?)++|-+|\S"

-+ 序列只匹配 1 个或多个 - 破折号,就像在 python re 模块表达式中一样,它没有什么特别的,真的。

现在,++ 之前 更有趣。这是possessive quantifier ,并且使用它可以防止正则表达式匹配器尝试模式的所有可能的排列。这是一种性能优化,可以防止 catastrophic backtracking问题。

关于Python正则表达式编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12237399/

24 4 0
文章推荐: php - Zend Framework 表关系
文章推荐: javascript - jquery 自动完成 |它出什么问题了
文章推荐: mysql - 我需要简化 MySQL 子查询以提高性能 - 请帮忙
文章推荐: python - 从 dbpedia 检索数据时出错
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com