Pages

Monday, September 13, 2010

The regular expression object depicts a pattern of characters.

Example 2

Here we will talk about 2 Properties (Global Property, IgnoreCase property) and 2 Methods (Execute Method, Replace Method) of Regular Expression object.

Global Property

Below is an example of Global property of VBScript Regular Expression object. Set the Global to True and it matches the whole string and shows all the occurrences of patterns in the string.

Function regexp_function(ptrn, strng)

Dim regexp, Match, Matches

'declaring Regular expression Object

Set regexp = New RegExp

'Setting pattern

regexp.Pattern = ptrn

regexp.IgnoreCase = True

regexp.Global = True

Set Matches = regexp.Execute(strng)

For Each Match in Matches

result = result & "Match is found at position "

result = result & Match.FirstIndex & vbCRLF

Next

regexp_function = result

End Function

Msgbox(regexp_function("i", " mIssIssippi"))

This above code when run shows the below message box.

clip_image002

CASE2:-If Global Property is set to False then the result will be

clip_image004(it takes the first occurrence of the char i)

IgnoreCase property

Sets or returns a Boolean value that indicates if a pattern search is case-sensitive or not. The value of the IgnoreCase property is False if the search is case-sensitive, True if it is not. Default is False.

Function regexp_function(ptrn, strng)

Dim regexp, Match, Matches

'declaring Regular expression Object

Set regexp = New RegExp

'Setting pattern

regexp.Pattern = ptrn

regexp.IgnoreCase = True

regexp.Global = True

Set Matches = regexp.Execute(strng)

For Each Match in Matches

result = result & "Match is found at position "

result = result & Match.FirstIndex & vbCRLF

Next

regexp_function = result

End Function

Msgbox(regexp_function("is", "missISsippi"))

For above code where IgnoreCase is true it shows

clip_image007

If IgnoreCase is False then it shows

clip_image010

Execute Method

The Execute method also takes one string parameter and returns a Matches collection containing a Match object for each match found in string,.Executes any statements defined as strings at runtime.

If the regex could not match the subject string at all, MatchCollection.Count will be zero.

If the RegExp.Global property is False (the default), MatchCollection will contain only the first match.

If RegExp.Global is true, MatchCollection will contain all matches.

You can see the examples of Global Property and IgnoreCase Property to get a feel of Execute method.

Replace Method

Its syntax is:- Object.Replace(string1, string2)

String1 is the text string in which the text replacement is to occur.

String2 is the replacement text string.

Function ReplaceTest(ptrn, replStr)

Dim regexp, str

str = "mississippi"

Set regexp = New RegExp

regexp.Pattern = ptrn

regexp.IgnoreCase = True

regexp.Global=True

ReplaceTest = regexp.Replace(str, replStr)

End Function

MsgBox(ReplaceTest("is", "x"))

'the above function call replaces "is" with "x"

The above code shows

clip_image013

No comments:

Post a Comment