是否有任何等效于 Python 中 Perl 正则表达式的 K 反斜杠序列? [英] Is there any equivalent to the Perl regexes' K backslash sequence in Python?
问题描述
Perl 的正则表达式具有 K
反斜杠序列:
K
这出现在 perl 5.10.0 中.K
左边的任何匹配都不是包含在 $&
中,如果该模式用于替代.这使您可以编写 s/PAT1 K PAT2/REPL/x
而不是s/(PAT1) PAT2/${1}REPL/x
或 s/(?<=PAT1) PAT2/REPL/x
.
助记符:保持.
Python 中有什么等价的东西吗?
建议替换 Python re
模块,可从 pypi
以 regex
名称获得,具有此功能.它的规范源存储库和错误跟踪器位于 在 bitbucket 中.
这是在 2015 年底添加的,位于 ticket 151;以该票证的使用为例:
<块引用>导入正则表达式为mrab>>>bsk = mrab.compile(r'start=>K.*')>>>打印(bsk.search('无聊的东西开始=>有趣的东西'))<regex.Match 对象;span=(20, 37), match='有趣的东西'>
Perl's regular expressions have the K
backslash sequence:
K
This appeared in perl 5.10.0. Anything matched left ofK
is not included in$&
, and will not be replaced if the pattern is used in a substitution. This lets you writes/PAT1 K PAT2/REPL/x
instead ofs/(PAT1) PAT2/${1}REPL/x
ors/(?<=PAT1) PAT2/REPL/x
.Mnemonic: Keep.
Is there anything equivalent in Python?
The proposed replacement to the Python re
module, available from pypi
under the name regex
, has this feature. Its canonical source repository and bug tracker are in bitbucket.
This was added in late 2015, in ticket 151; taking an example of its use from that ticket:
import regex as mrab >>> bsk = mrab.compile(r'start=>K.*') >>> print(bsk.search('boring stuff start=>interesting stuff')) <regex.Match object; span=(20, 37), match='interesting stuff'>
这篇关于是否有任何等效于 Python 中 Perl 正则表达式的 K 反斜杠序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!