Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regex replace in context

by John M. Dlugosz (Monsignor)
on Dec 11, 2002 at 16:41 UTC ( [id://219119]=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I have a couple places where I want to find /abc/ and replace b with d (actually they are more complex expressions, but this is the form it boils down to). So I added parens and wrote s/(a)b(c)/$1d$2/.

But I realized that this is what that "zero width lookbehind assertion" must be for. So, I'm going to review the perlre doc and learn how to use these extensions.

One reason I never bothered with them is that the docs are confusing.

But, to get started, am I correct in thinking that the equivilent would be s/(?<=a)b(?=c)/d/ ? And is that really any clearer?! What does "Works only for fixed-width look-behind" mean? Does that mean I can't have modifiers like + in there? In that case, what's the point, beyond saving a capture number in the cases where it does work?

—John

Replies are listed 'Best First'.
Re: Regex replace in context
by Aristotle (Chancellor) on Dec 11, 2002 at 17:00 UTC
    It does indeed mean you canīt have quantifiers in the lookbehind. It's mostly useful for single-character assertions or fixed tokens. It's also more useful in a simple match rather than a substitution.

    Makeshifts last the longest.

      It's also more useful in a simple match rather than a substitution.

      I don't see it. "Find a b only if its following an a" is just a normal search! If /ab/ matches (I'm only interested in "did it?", then /b/ must also be true. I don't need to dress it up at all. Or, if the starting position is what's important, then /a\Gb/ I think.

        I don't have a good example off hand, but it's more interesting toward the end of a complex variable pattern (esp with /g ) where you just want to exclude a few fringe conditions without extraordinarily complicating the rest of the match. Granted, it doesn't get much use - I think I needed it twice ever maybe - but it's nifty to have around if it doesn't add much weight.

        Makeshifts last the longest.

        True, but if you have a regex of the type "a pattern" followed by "another pattern which might contain the first pattern" and you want to find all occurrences, you have to use the look forward assertion or otherwise you have "used up" the second pattern and you will miss it.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Regex replace in context
by jkahn (Friar) on Dec 11, 2002 at 18:39 UTC
    FWIW, it also means that $& does not include a.

    I find the negative zero-width-lookbehind assertion rather handy (e.g. s/(?<!z)b/d/), which means, roughly, "substitute b for d whenever it's not preceded by z".

    This trick reminds me of the reason we have \b -- sometimes a boundary condition can be met by no character at all, e.g., we'd like to match on both "b" and "ab".

    In both cases, I've taken advantage of the not-modifying-$& effects in my code. I wish you could add the quantifiers, though, even though I can imagine just how complicated look-behinds with quantifiers might become...

    jyust my $0.02,

    --jeremy

      I suppose it would be clearer than (?<!(?i-x:pattern))

      Idle thought:

      sub import { shift; return unless @_; die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant'; overload::constant qr => sub { my $s= shift; $s =~ s/(<!\\)\x{ab}/(?:/g && $s =~ s/(<!\\)\x{bb}/)/g; return eval qr/$s/; }; }
      —John

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-16 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found