Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "?:^" regular expression mean?

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.

like image 620
levik Avatar asked Mar 02 '09 16:03

levik


3 Answers

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.

like image 56
Tomalak Avatar answered Sep 19 '22 13:09

Tomalak


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.

like image 26
Daniel LeCheminant Avatar answered Sep 19 '22 13:09

Daniel LeCheminant


(?: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

like image 3
Eli Avatar answered Sep 19 '22 13:09

Eli