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.
Regular expression to match a line that doesn't contain a word. 1650. 1807.
*? 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: . *? , .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With