1)To validate any website address starting from --www
set regex=new regexp
regex.pattern="www\..*\.(com|in|jp)$"
n=inputbox("validate any website address starting from --www")
if regex.test(n) then
msgbox "valid"
else
msgbox "invalid"
End if
2) Replace all matches in the string with the replacement text
Sub ReplaceAllByExpression(ByRef StringToExtract, ByVal MatchPattern, _ByVal ReplacementText)
Dim regEx, CurrentMatch, CurrentMatches
Set regEx = New RegExp
regEx.Pattern = MatchPattern
regEx.IgnoreCase = True
regEx.Global = True
regEx.MultiLine = True
StringToExtract = regEx.Replace(StringToExtract, ReplacementText)
Set regEx = Nothing
End Sub
' Example
' Remove all images from an HTML page
ReplaceAllByExpression HTMLPageData, "<img[^<]*?>", ""
The RegExp object has three properties and three methods. The properties are:
Set re = New RegExp
With re
.Pattern = "some_pattern"
.Global = True
.IgnoreCase = True
End With
A Pattern can be any string value.
For example, if the pattern is "Hello World", the RegExp object will match that in the target string.
If IgnoreCase is True, it will match any case, so "hellO wORld" would be matched.
If Global is set to True, it will contine to search the string for all instances of "Hello World". If False, it will stop searching after the first instance is found.
Execute method, returning Matches collection
Dim re, targetString, colMatch, objMatch
Set re = New RegExp
With re
.Pattern = "a"
.Global = True
.IgnoreCase = True
End With
targetString = "The rain in Spain falls mainly in the plain"
Set colMatch = re.Execute(targetString)
For each objMatch in colMatch
Response.Write objMatch.Value & "<br />"
Next
The above will produce a list of 5 letter a's.
Test method, returning True or False
Dim re, targetString
Set re = New RegExp
With re
.Pattern = "a"
.Global = False
.IgnoreCase = False
End With
targetString = "The rain in Spain falls mainly in the plain"
re.Test(targetString)
set regex=new regexp
regex.pattern="www\..*\.(com|in|jp)$"
n=inputbox("validate any website address starting from --www")
if regex.test(n) then
msgbox "valid"
else
msgbox "invalid"
End if
2) Replace all matches in the string with the replacement text
Sub ReplaceAllByExpression(ByRef StringToExtract, ByVal MatchPattern, _ByVal ReplacementText)
Dim regEx, CurrentMatch, CurrentMatches
Set regEx = New RegExp
regEx.Pattern = MatchPattern
regEx.IgnoreCase = True
regEx.Global = True
regEx.MultiLine = True
StringToExtract = regEx.Replace(StringToExtract, ReplacementText)
Set regEx = Nothing
End Sub
' Example
' Remove all images from an HTML page
ReplaceAllByExpression HTMLPageData, "<img[^<]*?>", ""
The RexExp Object
The RegExp object has three properties and three methods. The properties are:
- Pattern property - holds the regular expression pattern
- Global property - True or False (default False). If False, matching stops at first match.
- IgnoreCase property - True or False (default True). If True, allows case-insensitive matching
The methods are:
- Execute method - executes a match against the specified string. Returns a Matches collection, which contains a Match object for each match. The Match object can also contain a SubMatches collection
- Replace method - replaces the part of the string found in a match with another string
- Test method - executes an attempted match and returns True or False
To set up a RegExp object:
Dim reSet re = New RegExp
With re
.Pattern = "some_pattern"
.Global = True
.IgnoreCase = True
End With
A Pattern can be any string value.
For example, if the pattern is "Hello World", the RegExp object will match that in the target string.
If IgnoreCase is True, it will match any case, so "hellO wORld" would be matched.
If Global is set to True, it will contine to search the string for all instances of "Hello World". If False, it will stop searching after the first instance is found.
Execute method, returning Matches collection
Dim re, targetString, colMatch, objMatch
Set re = New RegExp
With re
.Pattern = "a"
.Global = True
.IgnoreCase = True
End With
targetString = "The rain in Spain falls mainly in the plain"
Set colMatch = re.Execute(targetString)
For each objMatch in colMatch
Response.Write objMatch.Value & "<br />"
Next
The above will produce a list of 5 letter a's.
Test method, returning True or False
Dim re, targetString
Set re = New RegExp
With re
.Pattern = "a"
.Global = False
.IgnoreCase = False
End With
targetString = "The rain in Spain falls mainly in the plain"
re.Test(targetString)
No comments:
Post a Comment