Showing posts with label VB Script Regular Expressions Utility Functions. Show all posts
Showing posts with label VB Script Regular Expressions Utility Functions. Show all posts

27 May 2013

VB Script Regular Expressions Utility Functions

VB Script Regular Expressions Utility Functions

Function to check if a string matches a Regular Expression Pattern

Function IsRegExMatch(strText, regEx_Pattern, blnMatchCompleteString)

Dim oRegExp, retVal

'‘Create regular expression object.

Set oRegExp = New RegExp

'‘Set pattern.

oRegExp.Pattern = regEx_Pattern

'‘Ignore case

oRegExp.IgnoreCase = True

'‘Match complete string or not

oRegExp.Global = blnMatchCompleteString

'‘Test the regular expression

IsRegExMatch = oRegExp.Test(strText)

Set oRegExp = Nothing

End Function

msgbox IsRegExMatch("welcome to vb script", "welcome", blnMatchCompleteString)

Function to Match a Regular Expression and Replace with a particular text if a string matches a regular expression pattern.

Function RegExMatchandReplace(strText,regEx_Pattern,strReplacementText,blnMatchCompleteString)

Dim oRegExp, retVal,strReplacedText

'‘Create regular expression object.
Set oRegExp = New RegExp

'‘Set pattern.
oRegExp.Pattern = regEx_Pattern
'‘Ignore case
oRegExp.IgnoreCase = True
'‘Match complete string or not
oRegExp.Global = blnMatchCompleteString

'‘Test the regular expression

RegExMatch = oRegExp.Test(strText)

If RegExMatch = True Then

strReplacedText = oRegExp.Replace(strText,strReplacementText)

End If

RegExMatchandReplace = strReplacedText

Set oRegExp = Nothing

End Function

msgbox RegExMatchandReplace("welcome to vb script", "welcome", "ending",blnMatchCompleteString)

Function to Get Total Number of Matches of a Regular Expression Pattern in a particular string

Function GetRegExMatchCount(strText, regEx_Pattern, blnMatchCompleteString)
Dim oRegExp, retVal
‘Create regular expression object.
Set oRegExp = New RegExp

‘Set pattern.
oRegExp.Pattern = regEx_Pattern
‘Ignore case
oRegExp.IgnoreCase = True
‘Match complete string or not
oRegExp.Global = blnMatchCompleteString

‘Test the regular expression
RegExpMatch = oRegExp.Test(strText)
If RegExpMatch Then
Set oRegExMatchCount = oRegExp.Execute(strText)
GetRegExMatchCount = oRegExMatchCount.Count
End If
Set oRegExp = Nothing
End Function

Function to Trim white space characters from end of a string

Public Function RTrimW(ByVal Text)
‘String pattern ending with carriage return, tab, new line or a space character
RTrimPattern = “[\s]*$”

Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Pattern =RTrimPattern
oRegExp.Global = False
RTrimW = oRegExp.Replace(Text,”")
Set oRegExp = Nothing
End Function

Function to Trim white space characters from start of a string

Public Function LTrimW(ByVal Text)
‘String pattern ending with carriage return, tab, new line or a space character
LTrimPattern = “^[\s]*”
Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Pattern = LTrimPattern
oRegExp.Global = False
LTrimW = oRegExp.Replace(Text,”")
Set oRegExp = Nothing
End Function

Function to Trim white space characters from both start and end of a string

Public Function TrimW(ByVal Text)
TrimW = LTrimW(RTrimW(Text))
End Function