Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: problem with substitution regex

by hdp (Beadle)
on Apr 27, 2001 at 03:29 UTC ( [id://75967] : note . print w/replies, xml ) Need Help??


in reply to problem with substitution regex

MrNobo1024's answer is unfortunately incorrect. It will match and replace the $fo part of $foo[5], for example.

You need to stop that by adding \w to the negative lookahead. s/(?<!\\)\$\w+(?![\[{\w])/whatever()/ge

I've replaced [a-zA-Z0-9_] with the more compact and equivalent \w, and tested for $bar{baz} as well.

This is still not perfect; it will munge $foo [5], which is valid (if rare) syntax. The easiest way to fix that is to use another regex, e.g. s/(\w) +([\[{])/$1$2/g

hdp.

Replies are listed 'Best First'.
Re: Re: problem with substitution regex
by MeowChow (Vicar) on Apr 27, 2001 at 03:41 UTC
    I believe that is why MrNobo1024 had added the \b.
      After I posted my message. :)

      hdp.