Regex: VBA : Evaluation of Expression with Conditions

Viewed 62

I am trying to extract a cell number from the formula expression (in vba) which I need to replace by another cell number. eg: I have the following formulae in different cells "=AL82+L8+L82", "=L8+L82" and "=AL82+L8" . I have to change "L8" in each of the formulae to "L9". I am new to Regex and was trying the following expression in regex pattern:

"(?=[^A-Z])([L8])(?=[^0-9])"

However only 8 is changed to L9. Please assist me with the error.

Thanks

1 Answers

You can capture either plus or an equals sign in a capturing group.

Then Match L8 and assert using a negative lookahead, that the 8 is not directly followed by a digit.

In the replacement use group 1 followed by L9: $1L9

([+=])L8(?!\d)

See a regex demo

Related