Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > microsoft.public.scripting.vbscript > #11176

Re: Regex Replace() with modified subexpressions?

Newsgroups microsoft.public.scripting.vbscript
Subject Re: Regex Replace() with modified subexpressions?
From "Evertjan." <exxjxw.hannivoort@inter.nl.net>
References <c62dc780-0b6a-44de-be21-7319105757ae@googlegroups.com>
Date 2015-11-04 09:32 +0100
Message-ID <XnsA54861021178Aeejj99@194.109.6.166> (permalink)

Show all headers | View raw


Jim <google@zolx.com> wrote on 04 Nov 2015 in
microsoft.public.scripting.vbscript: 

> 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.

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

> 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)

Back to microsoft.public.scripting.vbscript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Regex Replace() with modified subexpressions? Jim <google@zolx.com> - 2015-11-03 19:45 -0800
  Re: Regex Replace() with modified subexpressions? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-11-04 09:32 +0100
  Re: Regex Replace() with modified subexpressions? "Steve" <cerberus40+usenet@gmail.com> - 2015-11-04 06:52 -0500
    Re: Regex Replace() with modified subexpressions? Jim <google@zolx.com> - 2015-11-05 01:09 -0800

csiph-web