在Python中,Perl正则表达式的\ K反斜杠序列是否等效? [英] Is there any equivalent to the Perl regexes' \K backslash sequence in Python?
问题描述
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/x
或s/(?<=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 writes/PAT1 \K PAT2/REPL/x
instead ofs/(PAT1) PAT2/${1}REPL/x
ors/(?<=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屋!