Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?: do in regex

Tags:

regex

I have a regex that looks like this

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

What is the ?:?

like image 963
Bimmy Avatar asked Sep 14 '10 03:09

Bimmy


People also ask

What does ?: Do in a regexp?

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.

What does \f mean in regex?

Definition and Usage The \f metacharacter matches form feed characters.


2 Answers

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.

like image 68
BoltClock Avatar answered Sep 22 '22 05:09

BoltClock


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 (

like image 23
codaddict Avatar answered Sep 23 '22 05:09

codaddict