是否有任何等效于 Python 中 Perl 正则表达式的 K 反斜杠序列? [英] Is there any equivalent to the Perl regexes' K backslash sequence in Python?

查看:23
本文介绍了是否有任何等效于 Python 中 Perl 正则表达式的 K 反斜杠序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl 的正则表达式具有 K 反斜杠序列:

<块引用>

K
这出现在 perl 5.10.0 中.K 左边的任何匹配都不是包含在 $& 中,如果该模式用于替代.这使您可以编写 s/PAT1 K PAT2/REPL/x 而不是s/(PAT1) PAT2/${1}REPL/xs/(?<=PAT1) PAT2/REPL/x.

助记符:保持.

Python 中有什么等价的东西吗?

解决方案

建议替换 Python re 模块,可从 pypiregex 名称获得,具有此功能.它的规范源存储库和错误跟踪器位于 在 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 of K is not included in $&, and will not be replaced if the pattern is used in a substitution. This lets you write s/PAT1 K PAT2/REPL/x instead of s/(PAT1) PAT2/${1}REPL/x or s/(?<=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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆