13

I am looking at this sub-expression (this is in JavaScript):

(?:^|.....)

I know that ? means "zero or one times" when it follows a character, but not sure what it means in this context.

levik
  • 114,835
  • 27
  • 73
  • 90

6 Answers6

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.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
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.

Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
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).

See http://www.regular-expressions.info/brackets.html

Eli
  • 5,500
  • 1
  • 29
  • 27
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.

Trampas Kirk
  • 1,436
  • 3
  • 16
  • 21
2

"(?:x) Matches 'x' but does not remember the match."

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions

vartec
  • 131,205
  • 36
  • 218
  • 244
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.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132