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 * quantifier matches the preceding element zero or more times.
Perl provides several numbers of regular expression quantifiers which are used to specify how many times a given character can be repeated before matching is done. This is mainly used when the number of characters going to be matched is unknown.
$1 equals the text " brown ".
m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.
Because *
quantifier makes the preceding atom match zero or more times.
「」
is first match of / a * /
in any string. For example:
say "xabc" ~~ / a * . /; # OUTPUT: 「x」
it's same:
say "xabc" ~~ / (a+)? . /;
If you set the pattern more precise, you will get another result:
say "xabc" ~~ / x a * /; # OUTPUT: 「xa」
say "xabc" ~~ / a * b /; # OUTPUT: 「ab」
The answers here are correct, I'll just try to present them in a more coherent form:
The regex engine always starts at the left of the strings, and prefers left-most matches over longer matches
*
matches empty stringsThe 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.
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.
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