What is the behavior of (.) + In regular expression?

We just discovered an error in some code where the programmer used the equivalent (.)+ When they were supposed to use (.+) . A simple fix, but we cannot explain the behavior of (.)+ . Can someone explain why this matches "e", the last letter, not "b", the first letter after "a" in the regular expression? How would you explain (.)+ ?

 my $s = 'abcde'; if ($s =~ m{ a (.)+ }x ){ print "s '$s' matched '$1'\n"; }else{ print "total match fail\n"; } __END__ output: s 'abcde' matched 'e' 
+6
source share
1 answer

There is a huge difference between (.)+ And (.+) , But only from the point of view of the captured, and not with what corresponds.

(.)+ searches for one or more instances of the same character and captures the last of them.

(.+) searches for one or more single characters and simultaneously captures them all.

+10
source

All Articles