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.
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.
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.