I have these two bits of code that I thought should be equivalent. The first one uses the <|w>
to specify a word boundary where the non-word character (or start of string) should be before H. The second example uses the <<
, which should do the same thing.
my $string = 'Hamadryas perlicus';
say $string ~~ /
<?after <|w> Hamadryas \s+ >
(\w+)
/;
say $string ~~ /
<?after << Hamadryas \s+ >
(\w+)
/;
The first one matches but the second one doesn't:
「perlicus」
0 => 「perlicus」
Nil
Is there some other difference in these two?
A non-word boundary matches any place else: between any pair of characters, both of which are word characters or both of which are not word characters. at the beginning of a string if the first character is a non-word character. at the end of a string if the last character is a non-word character.
Word Boundary: \b The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore—but see below for variations across engines) and the other side is not a word character (for instance, it may be the beginning of the string or a space character).
The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”.
Using regex \B-\B matches - between the word color - coded . Using \b-\b on the other hand matches the - in nine-digit and pass-key .
This answer by timotimo in the IRC channel gives a hint of why that's happening that way. When you're using after
, you're actually flipping the regular expression. You'll then have to flip right for left, and that will work.
use v6;
my $string = 'Hamadryas perlicus';
say $string ~~ /
<?after Hamadryas <|w> \s+ >
(\w+)
/;
say $string ~~ /
<?after Hamadryas « \s+ >
(\w+)
/;
That will yield what you are looking for.
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