正则表达式中?:的文档? [英] Documentation for ?: in regex?

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

问题描述

前一段时间,我在regex中(至少在PHP中)看到,可以通过在?:之前添加一个捕获组来使其不捕获.

A while ago, I saw in regex (at least in PHP) you can make a capturing group not capture by prepending ?:.

$str = 'big blue ball';
$regex = '/b(ig|all)/';
preg_match_all($regex, $str, $matches);
var_dump($matches);

输出...

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(3) "big"
    [1]=>
    string(4) "ball"
  }
  [1]=>
  array(2) {
    [0]=>
    string(2) "ig"
    [1]=>
    string(3) "all"
  }
}

在此示例中,我不在乎括号中的匹配项,因此我附加了?:('/b(?:ig|all)/')并得到了输出

In this example, I don't care about what was matched in the parenthesis, so I appended the ?: ('/b(?:ig|all)/') and got output

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(3) "big"
    [1]=>
    string(4) "ball"
  }
}

这非常有用-至少我是这么认为的.有时,您只是不想用不必要的值来使比赛混乱.

This is very useful - at least I think so. Sometimes you just don't want to clutter your matches with unnecessary values.

我正在尝试查找文档及其正式名称(我称其为非捕获组,但我想我以前听过它.)

I was trying to look up documentation and the official name for this (I call it a non capturing group, but I think I've heard it before).

作为符号,对于Google来说似乎很难.

Being symbols, it seemed hard to Google for.

我也查看了许多正则表达式参考指南,没有提及.

I have also looked at a number of regex reference guides, with no mention.

?为前缀,并且出现在括号内的第一个字符中,使我相信它与先行或后退有关.

Being prefixed with ?, and appearing in the first chars inside parenthesis would leave me to believe it has something to do with lookaheads or lookbehinds.

那么,这些的专有名称是什么,我在哪里可以学到更多信息?

So, what is the proper name for these, and where can I learn more?

推荐答案

可在子模式页面.

普通括号具有两个功能的事实并不总是有用的.很多时候,需要分组子模式而不需要捕获.如果左括号后面跟有?:",则该子图案不执行任何捕获,并且在计算任何后续捕获子图案的数量时不进行计数.例如,如果字符串白色皇后"与模式匹配,则捕获的子字符串((?:red | white)(king | queen))为白色皇后"和女王",并分别编号为1和2 .捕获的子字符串的最大数量为99,所有子模式(捕获的和非捕获的)的最大数量为200.

The fact that plain parentheses fulfill two functions is not always helpful. There are often times when a grouping subpattern is required without a capturing requirement. If an opening parenthesis is followed by "?:", the subpattern does not do any capturing, and is not counted when computing the number of any subsequent capturing subpatterns. For example, if the string "the white queen" is matched against the pattern the ((?:red|white) (king|queen)) the captured substrings are "white queen" and "queen", and are numbered 1 and 2. The maximum number of captured substrings is 99, and the maximum number of all subpatterns, both capturing and non-capturing, is 200.

还要注意,您可以使用它来设置子模式的选项.例如,如果只希望子模式不区分大小写,则可以执行以下操作:

It's also good to note that you can set options for the subpattern with it. For example, if you want only the sub-pattern to be case insensitive, you can do:

(?i:foo)bar

将匹配:

  • foobar
  • Foobar
  • FoObar
  • ...等

但不是

  • fooBar
  • FooBAR
  • ...等

哦,虽然官方文档中实际上并没有明确地命名该语法,但它确实在以后将其称为非捕获子模式"(这是完全有道理的,无论如何我都会称呼它,因为它实际上不是一个组",而是一个子模式)...

Oh, and while the official documentation doesn't actually explicitly name the syntax, it does refer to it later on as a "non-capturing subpattern" (which makes complete sense, and is what I would call it anyway, since it's not really a "group", but a subpattern)...

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

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