in reply to Look Behind issues

This approach is a variation on AnonyMonk's above:

c:\@Work\Perl>perl -wMstrict -le "use 5.010; ;; my $s = qq{Please;, read these&\x23xB0;, before you post 180;, if not + absolutely &\x23x2013;, sure.}; print qq{'$s'}; ;; my $entity = qr{ & [\x23] x [[:xdigit:]]+ ; }xms; ;; $s =~ s{ (?: $entity , (*SKIP) (*FAIL))? ; \K , }{}xmsg; print qq{'$s'}; " 'Please;, read these°, before you post 180;, if not absolutely &# +x2013;, sure.' 'Please; read these°, before you post 180; if not absolutely &#x2 +013;, sure.'

Some notes (update: now with more numbers for your indexing enjoyment):

  1. My REPL does not like the # character at all, so it has to be represented as  \x23 in strings and regexes, but it prints and otherwise operates as expected;
  2. The substitution regex uses features added with Perl version 5.10:  (*SKIP) (*FAIL) (see Special Backtracking Control Verbs), and  \K (see Extended Patterns);
  3. The  $entity regex is very naive (as is the one used by AnonyMonk) and you will have to come up with one that covers all your cases;
  4. The  (?: $entity , (*SKIP) (*FAIL))? sub-pattern acts as a variable width negative look-behind by 'consuming' (i.e., skipping past) something that is needed for a subsequent match, in this case a comma following an entity.


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^2: Look Behind issues
by dominic01 (Sexton) on Mar 05, 2015 at 15:40 UTC
    This worked well for me. I learned something new (*SKIP) (*FAIL). Thank you