perl regex wildcard not working on sub pattern grouping

user2763829

I have tried using sub pattern to match multiple occurrence of a word using wild card but it is not working. I tried to replace * with ? but still does not match anything. Only works when I used + and {1,} to specify the number of occurrence.

Can anyone explain?

$_ = "Larry has a  camel little little little camel has a little camel";
print "Matched:--$`<<$&>>$'--\n" if /(little )*/;    

expected Matched:--Larry has a camel <<little little little >>camel has a little camel--

output Matched:--<<>>Larry has a camel little little little camel has a little camel--

Amadan

You are trying to find the first place where there is zero or more repetitions of "little ". Zero repetitions of anything is an empty string, which can be found anywhere - and in fact, the first such place is at the start of the string. In fact, even "big" has four places where you can find zero or more repetitions of "little " (the first one being, of course, at the start of the string).

If you change * "zero or more" to + "one or more", it should work as you wish it to, as you found.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related