tags:

views:

89

answers:

3

I have a regex that looks like this

/^(?:\w+\s)*(\w+)$*/

What is the ?:?

+7  A: 

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s), even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.

You're still looking for a specific pattern (in this case, a single whitespace character following at least one word), but you don't care what's actually matched.

BoltClock
+1 more info: http://www.regular-expressions.info/brackets.html
Dave DeLong
+1  A: 

It means only group but do not remember the grouped part.

By default ( ) tells the regex engine to remember the part of the string that matches the pattern between it. But at times we just want to group a pattern without triggering the regex memory, to do that we use (?: in place of (

codaddict
+2  A: 

Further to the excellent answers provided, its usefulness is also to simplify the code required to extract groups from the matched results. For example, your (\w+) group is known as group 1 without having to be concerned about any groups that appear before it. This may improve the maintainability of your code.

Christopher Hunt