Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Look Behind issues

by AnomalousMonk (Archbishop)
on Mar 05, 2015 at 06:25 UTC ( [id://1118840]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1118840]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found