Re: I need regex last instance help
[email protected] (ToddAndMargo via perl6-users) Mon, 27 Oct 2025 14:25:56 -0700
| Newsgroups | perl.perl6.users |
|---|---|
| Message-ID | <[email protected]> |
On 10/27/25 8:21 AM, William Michels wrote: > Hi Todd, > > Start by noting the `:g` "global" adverb (named-parameter). > This means Raku will try to match the regex pattern against input > multiple times. > Basically Raku will start from the left end of the input string, and try > to find a match. > The match will continue until the regex pattern fails. > Then (because of the :global adverb), Raku moves down a character and > starts all over again. > > Without the zero-width start/end-of-line/string assertions, this is what > you get: > > [0] > .Str.say for "first line\nsecond line" ~~ m:g/ \w+ / > first > line > second > line > > See remaining comments inline: > >> On Oct 26, 2025, at 21:35, ToddAndMargo via perl6-users <perl6- >> [email protected]> wrote: >> >> On 10/26/25 8:51 PM, William Michels wrote: >>> In the Raku REPL: >>> [0] > .Str.say for "first line\nsecond line" ~~ m:g/ ^ \w+ / >>> first >>> [0] > .Str.say for "first line\nsecond line" ~~ m:g/ ^^ \w+ / >>> first >>> second >>> [0] > .Str.say for "line first\nline second" ~~ m:g/ \w+ $ / >>> second >>> [0] > .Str.say for "line first\nline second" ~~ m:g/ \w+ $$ / >>> first >>> second >>> HTH, Bill. >>>> On Oct 26, 2025, at 14:50, ToddAndMargo via perl6-users <perl6- >>>> [email protected]> wrote: >>>> >>>> On 10/26/25 7:56 AM, William Michels wrote: >>>>> ^^ has the regex meaning "start-of-line" >>>>> $$ has the regex meaning "end-of-line" >>>>> Compare to: >>>>> ^ has the regex meaning "start-of-string" >>>>> $ has the regex meaning "end-of-string" >>>>> If you're analyzing input linewise (for example using a one-liner >>>>> with `-ne` or `-pe` flags), >>>>> then start-of-line equals start-of-string, and end-of-line equals >>>>> end- of-string. >>>>> HTH, Bill. >>>> >>>> >>>> Can I talk you out of a few examples? >> >> Hi Bill, >> >> Thank you! >> >> Would you double check my understanding? >> >> -T >> >> >> Extract the first (^) word (\w+) from the first line >> [0] > .Str.say for "first line\nsecond line" ~~ m:g/ ^ \w+ / >> first > > Above: CLOSE. Raku takes an input string, and is asked to find the `\w+` > characters immediately adjacent to the `^` start-of-string zero-width > assertion. There is only one "start-of-string". FYI, because you never > ask Raku to interrogate line-related information (e.g. match a `^^` or > `$$` or "\n" or "\v"), Raku has no idea whether the input string is one > line or many lines. > >> >> Extract the first (^^) word (\w+) from all the lines >> [0] > .Str.say for "first line\nsecond line" ~~ m:g/ ^^ \w+ / >> first >> second > > Above: CORRECT (although you might want to say "...from EACH of the lines"). > >> >> Extract the last ($) word (\w+) from the last line >> [0] > .Str.say for "line first\nline second" ~~ m:g/ \w+ $ / >> second > > Above: CLOSE. Raku takes an input string, and is asked to find the `\w+` > characters immediately adjacent to the `$` end-of-string zero-width > assertion. There is only one "end-of-string". Again, because you never > ask Raku to interrogate line-related information (e.g. match a `^^` or > `$$` or "\n" or "\v"), Raku has no idea whether the input string is one > line or many lines. > >> >> Extract the last ($$) word (\w+) from all the lines >> [0] > .Str.say for "line first\nline second" ~~ m:g/ \w+ $$ / >> first >> second > > Above: CORRECT (although you might want to say "...from EACH of the lines"). > > HTH, Bill. > Perfect! Thank you!