在正则表达式中捕获组 [英] capturing group in regex

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

问题描述

我正在探索在Regex中捕获群组,我对它缺乏文档感到困惑。对于前者,任何人都可以告诉我两个正则表达式之间的区别:

I am exploring capturing groups in Regex and I am getting confused about lack of documentation on it. For ex, can anyone tell me difference between two regex:

/(?:madhur)?/

/(madhur)?/

据我所知,?在第二个建议匹配 madhur 零或一次在字符串中。

As per me, ? in second suggests matching madhur zero or once in the string.

第一个与第二个不同的是什么?

How is the first different from second ?

推荐答案

第一个不存储捕获组,例如 $ 1 将为空。 ?:前缀使其成为非捕获组。这通常是为了获得更好的性能和反向引用的混乱。

The first one won't store the capturing group, e.g. $1 will be empty. The ?: prefix makes it a non capturing group. This is usually done for better performance and un-cluttering of back references.

在第二个示例中,捕获组中的字符将存储在后向引用中 $ 1

In the second example, the characters in the capturing group will be stored in the backreference $1.

进一步阅读

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

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