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

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

问题描述

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

Perl's regular expressions have the \K backslash sequence:

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

\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.

助记符:保持.

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

Is there anything equivalent in Python?

推荐答案

拟议替换Python re模块pypi以名称regex 提供的noreferrer>具有此功能.其规范的源代码存储库和错误跟踪器位于bitbucket中 .

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.

它是在2015年末的门票151 中添加的;以该票证中的用法为例:

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天全站免登陆