捕获和非捕获模式有什么区别? [英] What is the difference between capturing and non-capturing Pattern?

查看:157
本文介绍了捕获和非捕获模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模式类中,它表示有两种类型的正则表达式:捕获非捕获,但是我不明白其中的区别。

In the Pattern class it says there are two types of regex: capturing and non-capturing, but I don't understand the difference.

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#special

他们有什么不同?我何时必须使用每一个?任何例子?

How are they different? When do I have to use each one? Any examples?

推荐答案

考虑一种模式,你需要在一个位置检查各种事物,例如一堆不同的两个字符模式。通常你使用 | 交替运算符:

Consider a pattern where you need to check for a variety of things in a single position, e.g a bunch of different two character patterns. Normally you use the | alternation operator:

/(ab|cd|ef)/

需要使用()括号。但这些括号也可作为捕获组。也许你真的不想捕捉那些char序列,只是检查它们的存在,这是非捕获组发挥作用的地方:

which requires use of () brackets as well. But those brackets also act as a capturing group. Maybe you really don't want to capture those char sequences, just check for their presence, which is where the non-capturing groups come into play:

/(?:ab|cd|ef)/

这篇关于捕获和非捕获模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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