Discussion:
Regex Replace() with modified subexpressions?
(too old to reply)
Jim
2015-11-04 03:45:23 UTC
Permalink
I use VBScripts' regex Replace() method a lot to modify strings. It's great for rearranging and removing parts of a string, but I haven't yet found a way, if it's possible, to modify subexpressions found in a match when using Replace().

For example, in the following:

Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^.*\-\s*(.*)$"
s = "This is a Test - Keep only this part"
WScript.Echo s
s = re.Replace(s, "$1")
WScript.Echo s

I often want to do something like capitalize the matched $1 subexpression. It can be done with Execute() and some more code, but would be convenient if I could figure out how to form the second argument to Replace() so it could be done in just a line of code.
Evertjan.
2015-11-04 08:32:10 UTC
Permalink
Post by Jim
I use VBScripts' regex Replace() method a lot to modify strings. It's
great for rearranging and removing parts of a string, but I haven't yet
found a way, if it's possible, to modify subexpressions found in a match
when using Replace().
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^.*\-\s*(.*)$"
s = "This is a Test - Keep only this part"
WScript.Echo s
s = re.Replace(s, "$1")
WScript.Echo s
I often want to do something like capitalize the matched $1
subexpression.
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "[^\-]*\-\s*"
s = "This is a Test - Keep only this part"
WScript.Echo s
s = ucase(re.Replace(s, ""))
WScript.Echo s
Post by Jim
It can be done with Execute() and some more code, but
would be convenient if I could figure out how to form the second
argument to Replace() so it could be done in just a line of code.
This is easy in javascript, but am not sure about vbs:

var s = 'This is a Test - Keep only this part';
s = s.replace( /[^\-]*\-\s(.*)/,
function (x,a) {return a.toUpperCase()});
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Steve
2015-11-04 11:52:14 UTC
Permalink
"Jim" <***@zolx.com> wrote in message news:c62dc780-0b6a-44de-be21-***@googlegroups.com...

I use VBScripts' regex Replace() method a lot to modify strings. It's
great for rearranging and removing parts of a string, but I haven't yet
found a way, if it's possible, to modify subexpressions found in a match
when using Replace().

I often want to do something like capitalize the matched $1
subexpression. It can be done with Execute() and some more code, but
would be convenient if I could figure out how to form the second
argument to Replace() so it could be done in just a line of code.

MS added this to VBS with version 5.5 but only documented it in one
place:

https://msdn.microsoft.com/en-us/library/ms974619.aspx?f=255&MSPPError=-2147217396

The page is mostly about JScript but there is a "What About VBScript?"
section at the end which documents what you want to do.
--
Steve

In theory, there is no difference between theory and practice; in
practice, there is. -Chuck Reid
Jim
2015-11-05 09:09:05 UTC
Permalink
Post by Steve
MS added this to VBS with version 5.5 but only documented it in one
https://msdn.microsoft.com/en-us/library/ms974619.aspx?f=255&MSPPError=-2147217396
The page is mostly about JScript but there is a "What About VBScript?"
section at the end which documents what you want to do.
You're talking about the section about using a function pointer as the second argument to re.Replace(), correct?

That would clean up the code at the point where the re.Replace() is used, but seems to just move most of the complexities of using re.Execute() and sifting through matches and submatches collections into the referenced function.
Loading...