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

Re: regular expression matching array name

by hdp (Beadle)
on May 01, 2001 at 09:28 UTC ( [id://76878] : note . print w/replies, xml ) Need Help??

in reply to regular expression matching array name

/\$(\w+) *(?=\[)/ Given the example $foo[9], your regex first tries to match $foo, which fails because of the negative lookahead assertion (i.e. it is followed by [\w+]). Then it backtracks one character and tries again; $fo fits what it's looking for (\$(\w+), not followed by [\w+]), so that's what it matches.

Your lookahead won't match the huge variety of Perl expressions that could be used to index an array, but that's okay, because all you need is to check for the opening [ anyway.

It looks like you copied this code from somewhere else and don't really understand it; I can't think of any other reason you'd be using a negative lookahead there instead of a positive one.