develooper Front page | perl.perl6.users | Postings from May 2020

Regexps using 'after' and 'before' like ^ and $

Thread Next
From:
Joseph Brenner
Date:
May 25, 2020 21:10
Subject:
Regexps using 'after' and 'before' like ^ and $
Message ID:
CAFfgvXU+8eXp1oNfNs2AwGWOa845KhGFgJbdvOLW0B3LF64KtQ@mail.gmail.com
Given this string:
   my $str = "Romp romp ROMP";

We can match just the first or last by using the usual pinning
features, '^' or '$':

   say $str ~~ m:i:g/^romp/;               ## (「Romp」)
   say $str ~~ m:i:g/romp$/;               ## (「ROMP」)

Moritz Lenz (Section 3.8 of 'Parsing', p32) makes the point you
can use 'after' to do something like '^' pinning:

   say $str ~~ m:i:g/ <!after .> romp /;   ## (「Romp」)

That makes sense:  the BOL is "not after any character"
So: I wondered if there was a way to use 'before' to do
something like '$' pinning:

  say $str ~~ m:i:g/ romp <!before .> /;  ## (「Romp」 「romp」)

That was unexpected: it filters out the one I was trying to
match for, though the logic seemed reasonable: the EOL is "not
before any character".

What if we flip this and do a positive before match?

  say $str ~~ m:i:g/ romp <?before .> /;  ## (「Romp」 「romp」)

That does exactly the same thing, but here the logic makes
sense to me: the first two are "before some character",
but the last one isn't.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About