Link to home
Start Free TrialLog in
Avatar of Robert Berke
Robert BerkeFlag for United States of America

asked on

how to force $1 to be a literal instead of submatch when using regexp.replace method?

This is mainly for the regular expressions zone.

Line1 below works exactly as expected. It replaces the pattern with the text "works fine"
But, line2 fails, because it interprets the $1 as submatch #1 which is the word "quick".

line1: MsgBox RegExpReplace("the quick brown fox", "(quick|fast) brown", "works fine")
line2: MsgBox RegExpReplace("the quick brown fox", "(quick|fast) brown", "See Cell $A$1")

I tried using the standard escape character  "$A\$1" but that did not work.

I came up with the following workaround, but it is ugly, and I am sure there is a better way

msgbox replace(RegExpReplace("the quick brown fox", "(quick|fast) brown", "xxxxxx"),"xxxxxx","See Cell $A$1")

rberke

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That should work.  At least it did when I tested it just now :)

Adding that as a comment to https://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html, which is where that function came from...
Avatar of Robert Berke

ASKER

Thanks,

I knew there would be an easy way.

Bob
and thanks again to mathewspatrick for his wonderful function which I use every day.


Bob