* quantifier in Perl 6

  • A+

This seems to be something very basic that I don't understand here.

Why doesn't "babc" match / a * / ?

> "abc" ~~ / a / 「a」 > "abc" ~~ / a * / 「a」 > "babc" ~~ / a * / 「」                    # WHY? > "babc" ~~ / a + / 「a」 


The answers here are correct, I'll just try to present them in a more coherent form:

Matching always starts from the left

The regex engine always starts at the left of the strings, and prefers left-most matches over longer matches

* matches empty strings

The regex a* matches can match the strings '', 'a', 'aa' etc. It will always prefer the longest match it finds, but it can't find a match longer than the empty string, it'll just match the empty string.

Putting it together

In 'abc' ~~ /a*/, the regex engine starts at position 0, the a* matches as many a's as it can, and thus matches the first character.

In 'babc' ~~ /a*/, the regex engine starts at position 0, and the a* can match only zero characters. It does so successfully. Since the overall match succeeds, there is no reason to try again at position 1.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: