Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Regex question is this one of those look (ahead|behind)s ?

by kennethk (Abbot)
on Mar 25, 2014 at 18:20 UTC ( #1079716=note: print w/replies, xml ) Need Help??

in reply to Regex question is this one of those look (ahead|behind)s ?

use strict; use warnings; use YAPE::Regex::Explain; print YAPE::Regex::Explain->new(qr/^(?<!abc)def/)->explain;
tells us
The regular expression: (?-imsx:^(?<!abc)def) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- (?<! look behind to see if there is not: ---------------------------------------------------------------------- abc 'abc' ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- def 'def' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

Look aheads and look behinds are zero-width. This means that your regular expression is a subset of /^def/, which is clearly not what you intend. There are a number of ways to get what you express above. Probably the most natural would be s/(?<!^abc)\Kdef/xyz/. Moving the ^ inside the negative look behind means it only matters if the a starts the string. \K means 'keep everything before this' (see Character Classes and other Special Escapes)

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1079716]
[LanX]: obscure facts: only naturally born monks can become president here!
[cbeckley]: I have a functioning module I use to spawn sets of commands across multiple servers.
[cbeckley]: It's nascent, and probably looks like baby Perl to the veterans.

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2017-03-24 14:56 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (304 votes). Check out past polls.