python正则表达式“\1" [英] python regular expression "\1"

查看:80
本文介绍了python正则表达式“\1"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我以下 Python 正则表达式中的\1"是什么意思?

Can anyone tell me what does "\1" mean in the following regular expression in Python?

re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')

推荐答案

\1 等价于 re.search(...).group(1),正则表达式中第一个括号分隔的表达式.

\1 is equivalent to re.search(...).group(1), the first parentheses-delimited expression inside of the regex.

这也是一个有趣的事实,部分原因是正则表达式在 Python 和其他编程语言中比所需的要慢得多由 CS 理论.

It's also, fun fact, part of the reason that regular expressions are significantly slower in Python and other programming languages than required to be by CS theory.

这篇关于python正则表达式“\1"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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