正则表达式模式中的 () 和 [] 之间有什么区别? [英] What's the difference between () and [] in regular expression patterns?

查看:35
本文介绍了正则表达式模式中的 () 和 [] 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将正则表达式的一部分包含在 ()(括号)中与在 [](方括号)中进行有什么区别?

What is the difference between encasing part of a regular expression in () (parentheses) and doing it in [] (square brackets)?

这是怎么做到的:

[a-z0-9]

与此不同:

(a-z0-9)

?

推荐答案

[] 表示字符类.() 表示一个捕获组.

[] denotes a character class. () denotes a capturing group.

[a-z0-9] -- a-z OR 0-9

(a-z0-9) -- 显式捕获 a-z0-9.没有范围.

(a-z0-9) -- Explicit capture of a-z0-9. No ranges.

a -- 可以被[a-z0-9]捕获.

a-z0-9 -- 可以被 (a-z0-9) 捕获,然后可以在替换中和/或稍后的表达式中引用.

a-z0-9 -- Can be captured by (a-z0-9) and then can be referenced in a replacement and/or later in the expression.

这篇关于正则表达式模式中的 () 和 [] 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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