Regular Expression question...

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Regular Expression question...

Post by rasimmer »

Curious to know if it's possible to do a different replace for each match in a string.

strText = "12345 John w3rser John 234rw John sdfasadg"

Dim objRegEx : Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.Global = TRUEobjRegEx.IgnoreCase = TrueobjRegEx.Pattern = "(John)."
strText = objRegEx.Replace(strText, "$1 Smith ")
WScript.Echo strText
So, this will replace John with John Smith in the string. Now , what if I wanted to give John a different last name for each instance:

arrLastNames = Array("Smith", "Jones", "Vierra", "Hicks")

Is there a way to step through each match as an index and do a replace? So, the first instance would be John Smith, the next John Jones, etc.
User avatar
Gyorgy Nemesmagasi
Posts: 50
Last visit: Wed Mar 16, 2022 12:58 pm

Regular Expression question...

Post by Gyorgy Nemesmagasi »

Hey Ras, is this what you are looking for?

Code: Select all

	

	Dim strText: strText = " Set ComputerName = Computer123 'It needs to be replaced"
	Dim objRegEx : Set objRegEx = CreateObject("VBScript.RegExp") 
objRegEx.Global = TRUE
objRegEx.IgnoreCase = True
objRegEx.Pattern = "(S*s*ComputerNames*=s*)(S+)"
	strText = objRegEx.Replace(strText, "$1NewValue")
WScript.Echo strText
	
User avatar
Gyorgy Nemesmagasi
Posts: 50
Last visit: Wed Mar 16, 2022 12:58 pm

Regular Expression question...

Post by Gyorgy Nemesmagasi »

If you processing a multiline string the situation is a bit differnet. You can
change the code if you have DOS text (the line break is CrLF):

Code: Select all

	
objRegEx.Multiline = True
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "^(s*WSIDs*=s*)(S+)s*$"
	
strText = objRegEx.Replace(strText, "$1NewWSID" & vbCrLf & "USER=SYSTEM")
	

The objRegEx.Multiline = True tells the regexp engine the ^ match all lines begining and $ match all lines end. This is a simplified meaning but in your situation is OK.

The pattern:
^ line begin
( submatch start
s white space char
* any times (0 is allowed)
WSID as the text WSID
s white space char
* any times (0 is allowed)
= as text =
s white space char
* any times (0 is allowed)
) stop submatch, it will be the $1
( submatch start
S non-white space char
+ at least ones
) submatch end, it will be the $2
s white space
* any times (0 is allowed)
$ end if the line

You can find the scripting reg exp engine's syntax definition on the MSDN:
http://msdn.microsoft.com/en-us/library ... S.85).aspx
and here is the .Net one (e.g. for PowerShell), it has more features:
http://msdn.microsoft.com/en-us/library ... S.71).aspx
This topic is 14 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked