This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html
Set by lizmat on 8 June 2022.
Tirifto Hello! Is there a way I can ask a regex to match the whole string, when the regex itself does not start and end with ^ and $, respectively? I thought of something like /^<$regex>$/, but is there perhaps a more ‘direct’ way? 12:52
lizmat "foo" ~~ / f / && $/.from == 0 would cover the start position 12:57
"foo" ~~ / f / && $/.from == 0 && $/.pos == "foo".chars 12:58
Tirifto Oh! 13:00
Thank you, lizmat! I didn’t realise those were available. :D Although the docs say that the output of .pos is unspecified; should I use .to instead? 13:03
lizmat yeah, probably should use .to :-) 13:04
m: sub infix:<~=~>($a, $b) { $a.match($b); $/.from == 0 && $/.to == $a.chars }; say "foo" ~=~ / foo / 13:11
camelia True
lizmat m: sub infix:<~=~>($a, $b) { $a.match($b); $/.from == 0 && $/.to == $a.chars }; say "foo" ~=~ / oo /
camelia False
lizmat another approach: 13:12
m: sub infix:<~=~>($a, $b) { $a.match($b); $/.from == 0 && $/.to == $a.chars ?? $/ !! Nil }; say "foo" ~=~ / foo /
camelia 「foo」
lizmat m: sub infix:<~=~>($a, $b) { $a.match($b); $/.from == 0 && $/.to == $a.chars ?? $/ !! Nil }; say "foo" ~=~ / oo /
camelia Nil
Tirifto Oh, that’s a nice idea! And it seems to fit in very well. 13:17
lizmat from a performance point of view, it may actually be better to use / ^ <$regex> $ / 13:19
with a small needle and a large haystack, the regex engine could potentially do a lot of work
whereas a ^ would allow it to stop immediately if the first character didn't match 13:20
so YMMV
Tirifto An excellent point, too. `o` 13:30
lizmat m: m: sub infix:<~=~>($a, $b) { $a.match(/ ^ <$b> $/) }; say "foo" ~=~ / oo / # maybe better
camelia Nil
lizmat m: m: sub infix:<~=~>($a, $b) { $a.match(/ ^ <$b> $/) }; say "foo" ~=~ / foo / # maybe better
camelia 「foo」