Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?: in a regular expression mean? [duplicate]

Please explain the meaning of this regular expression and what groups the expression will generate?

$string =~ m/^(\d*)(?: \D.*?)(\d*)$/

PS: I'm re-factoring Perl code to Java.

like image 715
Saravanan Avatar asked Oct 11 '14 11:10

Saravanan


People also ask

What is the meaning of ?: In regex?

Regular expression to match a line that doesn't contain a word. 1650. 1807.

What is the difference between * and *??

*? is non-greedy. * will match nothing, but then will try to match extra characters until it matches 1 , eventually matching 101 . All quantifiers have a non-greedy mode: . *? , .

What is the meaning of symbols * used in regular expression?

The asterisk ( * ): The asterisk is known as a repeater symbol, meaning the preceding character can be found 0 or more times. For example, the regular expression ca*t will match the strings ct, cat, caat, caaat, etc.


1 Answers

It means that it is not capturing group. After successful match first (\d*) will be captured in $1, and second in $2, and (?: \D.*?) would not be captured at all.

$string =~ m/^(\d*)(?: \D.*?)(\d*)$/

From perldoc perlretut

Non-capturing groupings

A group that is required to bundle a set of alternatives may or may not be useful as a capturing group. If it isn't, it just creates a superfluous addition to the set of available capture group values, inside as well as outside the regexp. Non-capturing groupings, denoted by (?:regexp), still allow the regexp to be treated as a single unit, but don't establish a capturing group at the same time.

like image 110
mpapec Avatar answered Oct 01 '22 07:10

mpapec