Regular Expressions

You can add an additional Reference
Microsoft VBScript Regular Expressions 5.5
C:\Windows\System32\vbscript.dll
link - learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/ms974570(v=msdn.10)
link - regexone.com

Dim sTextString As String 

Dim oRegEx As RegExp
Dim oRegExResults As Object
Set oRegEx = New RegExp
'Set oRegEx = CreateObject("VBScript.RegExp")

oRegEx.Global = True
oRegEx.MultiLine = True
oRegEx.IgnoreCase = False

'replacing text
Dim sNewString As String
sTextString = "bettersolutions"
oRegEx.Pattern = "better"
sNewString = oRegEx.Replace(sTextString, "amazing")
Debug.Print sNewString

'testing for a match, 2 digit numbers
Dim bfound As Boolean
sTextString = "1,2,30,4,5"
oRegEx.Pattern = "\d{2}"
bfound = oRegEx.Test(sTextString)
If (bfound = True) Then
   Debug.Print "match found"
End If

extracting all instances
Dim oFound As Object
sTextString = "one,two,three,four"
oRegEx.Pattern = "[a-z]+"
Set oRegExResults = oRegEx.Execute(sTextString)
For Each oFound In oRegExResults
   Debug.Print oFound
Next oFound

Dim oRegEx As VBScript_RegExp_55.RegExp 
Dim oRegExMatch As VBScript_RegExp_55.MatchCollection
Dim sTextString As String

Matching Exact 2 Characters

Set oRegEx = New VBScript_RegExp_55.RegExp 
sTextString = "one,two,three,four"
oRegEx.Pattern = "ee"
Set oRegExMatch = oRegEx.Execute(sTextString)
Debug.Print pRegExMatch.Count

Matching Any 3 Digits

The "\d" can match any digit, equivalent to [0-9]

oRegEx.Pattern = "\d\d\d" 

Matching Any 3 Characters

The "." will match any letter, digit, whitespace, everything except \n

oRegEx.Pattern = "..." 

Matching Any 3 Non Digits

The "\D" will match any non digit, equivalent to [^0-9]

oRegEx.Pattern = "\D" 

Matching A Full Stop

The "\." will match a full stop

oRegEx.Pattern = "\." 

Matching Certain Characters

The "[et]" will match single "e" and "t" characters

oRegEx.Pattern = "[et]" 

Matching Excluding Characters

The "[^et]" will match everything that doesn't contain the single characters "e" and "t"

oRegEx.Pattern = "[^et]" 

Matching Any Alphanumeric Characters

The "\w" will match all letters and numbers, equivalent to [A-Za-z0-9]

oRegEx.Pattern = "\w" 

Matching Any 3 Letters with the first one in uppercase

oRegEx.Pattern = "[A-Z][a-za-z]" 

Matching Repetition

This will match "aaaa"

oRegEx.Pattern = "a{4}" 

This will match ??

oRegEx.Pattern = "[abc]{2}" 

This will match any characters repeated 2,3 or 4 times

oRegEx.Pattern = ".{2,4}" 

Matching Zero or More Repetitions

The "*" will match zero or more repetitions

oRegEx.Pattern = "\d*" 

This will match any number of a's, b's and c's

oRegEx.Pattern = "a*b*c*" 

Matching One or More Repetitions

The "+" will match one or more repetitions

oRegEx.Pattern = "\d+" 

Matching Optional Characters

The "?" will match optional characters
This will make the "b" an optional character

oRegEx.Pattern = "ab?c" 

Matching Whitespace

The "\s" will match any single space, tab character, new line or carriage return character

oRegEx.Pattern = ".\.\s+abc" 

Matching Start and End

oRegEx.Pattern = "^error$" 


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext