Robert Berke
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
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
rberke
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks,
I knew there would be an easy way.
Bob
I knew there would be an easy way.
Bob
ASKER
and thanks again to mathewspatrick for his wonderful function which I use every day.
Bob
Bob
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...