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

”=====================================================================================================================================================================
”’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

”=====================================================================================================================================================================
”’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

Advertisements
By shalabhdixit

3 comments on “VB Script Regular Expressions Utility Functions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s