Lookahead/lookbehind assertions: maybe the mnemonic "ABBA" will help?
In Markdown: 'Use *A*fter for a look-*B*ehind, use *B*efore for a look-*A*head', or... 'For a look-*A*head' use *B*efore, for a look-*B*ehind" use *A*fter'. As a trivial example of the first mnemonic in practice, below are examples with html tags. Note the last two examples which differ in the use of a 'greedy' (.*) pattern vs the use of a 'frugal' (.*?) pattern, still give the same result: > my $title = '<title>abcdefghijklmnopqrstuvwxyz</title>'; <title>abcdefghijklmnopqrstuvwxyz</title> > $title ~~ /<?after '<title>' > .* <?before '</title>' >/; 「abcdefghijklmnopqrstuvwxyz」 > say ~$/ if $title ~~ /<?after '<title>' > .* <?before '</title>' >/; abcdefghijklmnopqrstuvwxyz > say ~$/ if $title ~~ /<?after '<title>' > .*? <?before '</title>' >/; abcdefghijklmnopqrstuvwxyz > HTH, Bill. https://docs.perl6.org/language/regexes On Fri, Aug 23, 2019 at 2:28 PM Brad Gilbert <b2gi...@gmail.com> wrote: > > `after` and `before` can be confusing, but I think it would be more confusing > if it were the other way around. > > On Fri, Aug 23, 2019 at 2:15 PM Sean McAfee <eef...@gmail.com> wrote: >> >> On Thu, Aug 22, 2019 at 6:11 PM yary <not....@gmail.com> wrote: >>> >>> Perl 6 is doing the right thing. The dot matches any character. In >>> this case, matching the final ':'. The next bit of the regex says the >>> cursor has to be after 1:, and indeed, after matching the ':' the >>> cursor is after '1:', so the substitution succeeds. >> >> >> My real use case, that I tried to provide a simplified example of, was to >> process some pretty-printed JSON. Less simplified this time, I wanted to >> change all "foo": "whatever" strings to "foo": "*". In Perl 5 I would have >> done: >> >> s/(?<="foo": ")[^"]+/*/; >> >> Trying to express this in Perl 6, I thought "lookbehind" would naturally >> translate to a "before" assertion: >> >> s/<?before '"foo": "'><-["]>+/*/; >> >> ...but that didn't work. Various other attempts led to the simplified >> example I originally provided. >> >> Long story short, it seems that a Perl 5 (?<=...) lookbehind translates to a >> Perl 6 <?after ...> assertion, and likewise a Perl 5 (?=...) lookahead >> translates to a Perl 6 <?before ...> assertion. The terminology just >> confused me due to my prior Perl 5 experience. >>