RegEx problem - literal exclusion should not match

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I'm having a problem with a reg ex. Essentiall, I'm trying to isolate
variable declarations from old vbscript where there was no explicit
declaration requirement. This should seem easy enough - just identify by the
assignment statement; except for the fact that vb uses a single '=' token for
comparison as well as assignment. Therefore, I need a regex that will match
assignment statements while excluding if statements.

I tried to do the following:
(?!if)\s*(\w+)\s*=\s*.*

but this statement matches both conditionals and assignments. Basically, I
need the following conditions to hold:
var="var" -> match
if var="var" -> no match

tia,

_howard
 
(?!if)\s*(\w+)\s*=\s*.*

You're using lookahead ?!, but should use lookback ?<!.

Try this:

(?<!if\s*)(\w+)\s*=

Took the liberty to skip the \s*.* at then end because it doesn't do
anything :-)

By the way, if there are more assignments, this regex would catch them all:

a = 1
a = a + 1

The second statement would also be changed into a declaration, which isn't
right, but the compiler will tell you about it.

Take a look at http://regex.osherove.com/, by the way, it's insanely
useful for regex testing.

Greetings,
Wessel
 
Back
Top