I am looking at this sub-expression (this is in JavaScript):
我正在看这个子表达式(这是在JavaScript中):
(?:^|.....)
I know that ? means "zero or one times" when it follows a character, but not sure what it means in this context.
我知道 ?当它跟随一个角色时意味着“零或一次”,但不确定它在这种情况下意味着什么。
6 个解决方案
#1
19
You're probably seeing it in this context
你可能在这种情况下看到了它
(?:...)
It means that the group won't be captured or used for back-references.
这意味着该组不会被捕获或用于反向引用。
EDIT: To reflect your modified question:
编辑:反映您修改后的问题:
(?:^|....)
means "match the beginning of the line or match ..." but don't capture the group or use it for back-references.
表示“匹配行的开头或匹配...”但不捕获该组或将其用于反向引用。
#2
21
When working with groups, you often have several options that modify the behavior of the group:
使用组时,通常有几个选项可以修改组的行为:
(foo) // default behavior, matches "foo" and stores a back-reference
(?:foo) // non-capturing group: matches "foo", but doesn't store a back-ref
(?i:foo) // matches "foo" case-insensitively
(?=foo) // matches "foo", but does not advance the current position
// ("positive zero-width look-ahead assertion")
(?!foo) // matches anything but "foo", and does not advance the position
// ("negative zero-width look-ahead assertion")
to name a few.
仅举几例。
They all begin with "?", which is the way to indicate a group modifier. The question mark has nothing to do with optionality in this case.
它们都以“?”开头,这是表示组修饰符的方式。在这种情况下,问号与可选性无关。
It simply says:
它只是说:
(?:^foo) // match "foo" at the start of the line, but do not store a back-ref
Sometimes it's just overkill to store a back-reference to some part of the match that you are not going to use anyway. When the group is there only to make a complex expression atomic (e.g. it should either match or fail as a whole), storing a back-reference is an unnecessary waste of resources that can even slow down the regex a bit. And sometimes, you just want to be group 1 the first group relevant to you, instead of the first group in the regex.
有时候,对于你不会使用的比赛的某些部分来说,存储一个反向引用是过分的。当组只存在一个复杂的表达式原子(例如,它应该匹配或作为一个整体失败)时,存储反向引用是不必要的资源浪费,甚至可以减慢正则表达式。有时候,你只想成为第一组与你相关的第一组,而不是正则表达式中的第一组。
#3
3
(?:some stuff)
means that you don't want to match the expression in the parentheses separately. Normally the pieces of a regexp grouped in parentheses are grouped and can be referenced individually (this is called using backreferences).
(?:some stuff)意味着你不想分别匹配括号中的表达式。通常,在括号中分组的正则表达式的片段被分组并且可以单独引用(这使用反向引用来调用)。
See http://www.regular-expressions.info/brackets.html
见http://www.regular-expressions.info/brackets.html
#4
3
Short Answer
It flags the (parenthetical) group as a non-capturing group.
它将(括号)组标记为非捕获组。
Details About This Particular Expression
The notation for a non-capturing group is:
非捕获组的表示法是:
(?:<expresson>)
In the instance you presented, the caret (^) is part of the expression not part of the capturing group notation. And this instance it's not a special character either.
在您提供的实例中,插入符号(^)是表达式的一部分,而不是捕获组表示法的一部分。而这个例子也不是一个特殊的角色。
It looks like they're using an 'or' operator (the pipe) with the caret. So they're looking to match something that is a caret or whatever was on the right of the pipe, but not capture the expression as a group (accomplished with the ?:
in the beginning of the grouping characters.
看起来他们正在使用带有插入符号的'或'运算符(管道)。因此,他们希望匹配管道右侧的插入符号或其他内容,但不能将表达式捕获为一个组(在分组字符的开头用?:完成。
In General
Non-capturing groups allow you to group an expression in a way that won't be back-refernceable, and will also increase performance of the expression.
非捕获组允许您以不可反向引用的方式对表达式进行分组,并且还可以提高表达式的性能。
#5
2
"(?:x) Matches 'x' but does not remember the match."
“(?:x)匹配'x'但不记得匹配。”
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
#6
1
?: Generally indicates making the group a non capture. You can do some research here.
?:通常表示使组成为非捕获组。你可以在这里做一些研究。
I'm almost positive any regex engine should but when I switch between engines I run into some quirks.
我几乎肯定任何正则表达式引擎应该但是当我在引擎之间切换时,我遇到了一些怪癖。
Edit: This should be the case, non captures seems to work fine.
编辑:应该是这种情况,非捕获似乎工作正常。
#1
19
You're probably seeing it in this context
你可能在这种情况下看到了它
(?:...)
It means that the group won't be captured or used for back-references.
这意味着该组不会被捕获或用于反向引用。
EDIT: To reflect your modified question:
编辑:反映您修改后的问题:
(?:^|....)
means "match the beginning of the line or match ..." but don't capture the group or use it for back-references.
表示“匹配行的开头或匹配...”但不捕获该组或将其用于反向引用。
#2
21
When working with groups, you often have several options that modify the behavior of the group:
使用组时,通常有几个选项可以修改组的行为:
(foo) // default behavior, matches "foo" and stores a back-reference
(?:foo) // non-capturing group: matches "foo", but doesn't store a back-ref
(?i:foo) // matches "foo" case-insensitively
(?=foo) // matches "foo", but does not advance the current position
// ("positive zero-width look-ahead assertion")
(?!foo) // matches anything but "foo", and does not advance the position
// ("negative zero-width look-ahead assertion")
to name a few.
仅举几例。
They all begin with "?", which is the way to indicate a group modifier. The question mark has nothing to do with optionality in this case.
它们都以“?”开头,这是表示组修饰符的方式。在这种情况下,问号与可选性无关。
It simply says:
它只是说:
(?:^foo) // match "foo" at the start of the line, but do not store a back-ref
Sometimes it's just overkill to store a back-reference to some part of the match that you are not going to use anyway. When the group is there only to make a complex expression atomic (e.g. it should either match or fail as a whole), storing a back-reference is an unnecessary waste of resources that can even slow down the regex a bit. And sometimes, you just want to be group 1 the first group relevant to you, instead of the first group in the regex.
有时候,对于你不会使用的比赛的某些部分来说,存储一个反向引用是过分的。当组只存在一个复杂的表达式原子(例如,它应该匹配或作为一个整体失败)时,存储反向引用是不必要的资源浪费,甚至可以减慢正则表达式。有时候,你只想成为第一组与你相关的第一组,而不是正则表达式中的第一组。
#3
3
(?:some stuff)
means that you don't want to match the expression in the parentheses separately. Normally the pieces of a regexp grouped in parentheses are grouped and can be referenced individually (this is called using backreferences).
(?:some stuff)意味着你不想分别匹配括号中的表达式。通常,在括号中分组的正则表达式的片段被分组并且可以单独引用(这使用反向引用来调用)。
See http://www.regular-expressions.info/brackets.html
见http://www.regular-expressions.info/brackets.html
#4
3
Short Answer
It flags the (parenthetical) group as a non-capturing group.
它将(括号)组标记为非捕获组。
Details About This Particular Expression
The notation for a non-capturing group is:
非捕获组的表示法是:
(?:<expresson>)
In the instance you presented, the caret (^) is part of the expression not part of the capturing group notation. And this instance it's not a special character either.
在您提供的实例中,插入符号(^)是表达式的一部分,而不是捕获组表示法的一部分。而这个例子也不是一个特殊的角色。
It looks like they're using an 'or' operator (the pipe) with the caret. So they're looking to match something that is a caret or whatever was on the right of the pipe, but not capture the expression as a group (accomplished with the ?:
in the beginning of the grouping characters.
看起来他们正在使用带有插入符号的'或'运算符(管道)。因此,他们希望匹配管道右侧的插入符号或其他内容,但不能将表达式捕获为一个组(在分组字符的开头用?:完成。
In General
Non-capturing groups allow you to group an expression in a way that won't be back-refernceable, and will also increase performance of the expression.
非捕获组允许您以不可反向引用的方式对表达式进行分组,并且还可以提高表达式的性能。
#5
2
"(?:x) Matches 'x' but does not remember the match."
“(?:x)匹配'x'但不记得匹配。”
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
#6
1
?: Generally indicates making the group a non capture. You can do some research here.
?:通常表示使组成为非捕获组。你可以在这里做一些研究。
I'm almost positive any regex engine should but when I switch between engines I run into some quirks.
我几乎肯定任何正则表达式引擎应该但是当我在引擎之间切换时,我遇到了一些怪癖。
Edit: This should be the case, non captures seems to work fine.
编辑:应该是这种情况,非捕获似乎工作正常。