Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Look Behind issues

by dominic01 (Sexton)
on Mar 05, 2015 at 04:22 UTC ( [id://1118831]=perlquestion: print w/replies, xml ) Need Help??

dominic01 has asked for the wisdom of the Perl Monks concerning the following question:

I have a line like
$Line = "Please;, read these°, before you post 180;, if you're not absolutely –, sure you're posting";

I am trying to replace the ";," with ";". However it should not replace if the comma follows an entity.

My output should be
$Line = "Please; read these°, before you post 180; if you're not absolutely –, sure you're posting";

Appreciate any help on framing the regex. I am using perl 5, version 20, subversion 1 (v5.20.1) built for MSWin32-x64-multi-thread

Replies are listed 'Best First'.
Re: Look Behind issues
by AnomalousMonk (Archbishop) on Mar 05, 2015 at 06:25 UTC

    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:  <%-(-(-(-<

      This worked well for me. I learned something new (*SKIP) (*FAIL). Thank you
Re: Look Behind issues
by Anonymous Monk on Mar 05, 2015 at 05:11 UTC
      This worked well for me. Thank you.
Re: Look Behind issues (just matching)
by Anonymous Monk on Mar 05, 2015 at 04:26 UTC

    How about no look behind?

    match entity # $1 commatarget # $2 and replace entity with itself and commatarget with comma, and you're +done :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found