• No se han encontrado resultados

Session 17: Regular Expression Pattern Match and

Replacement

Replacement

Learning Objectives

Learning Objectives

This Session provides tutorial examples and notes on regular expression support in VBScript. This Session provides tutorial examples and notes on regular expression support in VBScript. Topics include 'RegExp' class and objects, setting up regular expressions and match option flags, Topics include 'RegExp' class and objects, setting up regular expressions and match option flags, applying a pattern for matches and replacements, checking MatchCollection and SubMatches applying a pattern for matches and replacements, checking MatchCollection and SubMatches collection objects, example of regular expression pattern match and replacement.

collection objects, example of regular expression pattern match and replacement.

 "RegExp" Class and Object for Regular Expression Support"RegExp" Class and Object for Regular Expression Support 

 "MatchCollection" and "SubMatches" Collection Objects"MatchCollection" and "SubMatches" Collection Objects 

 "Set oRegExp = New RegExp" - Creating RegExp O"Set oRegExp = New RegExp" - Creating RegExp Objectsbjects 

 Example of Regular Expression Match and ReplacementExample of Regular Expression Match and Replacement

"RegExp" Class and Object for

"RegExp" Class and Object for Regular Expression SupportRegular Expression Support

VBScript supports a built-in class called "RegExp" to pr

VBScript supports a built-in class called "RegExp" to provide regular expression support.ovide regular expression support.

In order to perform a regular expression task, you need to create a RegExp object with this syntax: In order to perform a regular expression task, you need to create a RegExp object with this syntax:

Set oRegExp = New RegExp Set oRegExp = New RegExp

Then we are ready to use properties and methods supported on the RegExp object, oRegExp: Then we are ready to use properties and methods supported on the RegExp object, oRegExp:

oRegRex.Pattern - A String property containing the regular expression string to represent oRegRex.Pattern - A String property containing the regular expression string to represent the search pattern.

the search pattern.

oRegRex.Global - A Boolean property representing the global search flag. If turned on, the oRegRex.Global - A Boolean property representing the global search flag. If turned on, the search will be applied multiple times on the entire string.

search will be applied multiple times on the entire string.

oRegRex.IgnoreCase - A Boolean property representing the ignore-case search flag. If oRegRex.IgnoreCase - A Boolean property representing the ignore-case search flag. If turned on, the search will be applied case insensitively.

turned on, the search will be applied case insensitively.

oRegRex.Test (string) - A method returning True, if a pattern match is found in the given oRegRex.Test (string) - A method returning True, if a pattern match is found in the given string.

string.

oRegRex.Execute (string) - A method returning a MatchCollection collection object that oRegRex.Execute (string) - A method returning a MatchCollection collection object that contains a list of Match objects. Each Match object represents a pattern match found in contains a list of Match objects. Each Match object represents a pattern match found in the given string.

the given string.

oRegRex.Replace (string, replacement) - A method returning a copy of the given string oRegRex.Replace (string, replacement) - A method returning a copy of the given string with each pattern match found and replaced with the replacement string.

with each pattern match found and replaced with the replacement string. The Match object has the following properties:

The Match object has the following properties:

oMatch.Length - An integer property representing the number of characters of this pattern oMatch.Length - An integer property representing the number of characters of this pattern match.

match.

oMatch.FirstIndex - An integer property representing the position where this pattern match oMatch.FirstIndex - An integer property representing the position where this pattern match was found in the string.

was found in the string.

oMatch.Value - An String property representing this match found in the string. The "Value" oMatch.Value - An String property representing this match found in the string. The "Value" property is the default property. So the "Value" property will be returned if a Match object property is the default property. So the "Value" property will be returned if a Match object is used in a String context.

is used in a String context.

oMatch.SubMatches - A SubMatches collection property that contains a list of Strings. oMatch.SubMatches - A SubMatches collection property that contains a list of Strings. Each string represents a sub pattern match.

"MatchCollection" and "SubMatches" Collection Objects

We are mentioned that MatchCollection and SubMatches objects are collection objects. All collection objects share same properties and methods:

oCollection.Count - An integer property representing the number of items in this collection. oCollection.Item(i) - A method returning the item at position i in this collection. The "Item(i)" method is defined as the default method of a collection object. So you can call this method without the method name. For example, oCollection(0) is equivalent to oCollection.Item(0), referring to the first item in this collection.

A collection object can also be used like an array in a "For Each" statement: For Each e In oCollection

... = ... e ... ' e Represents the current item End For

If you apply the above properties and methods to a MatchCollection object, oMatches, returned from the oRegExp.Execute(string) method, you should understand the following interesting examples:

oMatches.Count - Number of Match objects. oMatches.Item(0) - The first Match object. oMatches(0) - The first Match object.

oMatches(1).SubMatches - The SubMatches collection object of the second Match object. oMatches(1).SubMatches.Count - The number of sub match strings of the second Match object.

oMatches(1).SubMatches.Item(1) - The second sub match string of the second Match object.

oMatches(1).SubMatches(1) - The second sub match string of the second Match object.

"Set oRegExp = New RegExp" - Creating RegExp Objects

1. Create an empty RegExp object with a "Set" statement and set it to a variable: Dim oRegExp

Set oRegExp = New RegExp

Where "RegExp" is the name of the "RegExp" class; "New" is an operator to create a new object out of a class; "oRegExp" is Variant variable holding the new "RegExp" object.

2. Assign a regular expression string as the match pattern following regular expression rules. Here are some examples of regular expressions - see a regular expression manual for more examples:

' A pattern to match any word ending with "ee". ' No sub matches defined.

oRegExp.Pattern = "\w+ee\s"

' A pattern to match any email address. ' 3 sub matches defined in the pattern. oRegExp.Pattern = "(\w+)@(\w+)\.(\w+)"

' Repeat matches on the entire string oRegExp.Global = True

' Ignore cases while matching the pattern oRegExp.IgnoreCase = True

Now the "RegExp" object, oRegExp, is ready to be used to match again any given string following operations:

' Returns True if the pattern matched at least once. bFound = oRegExp.Test(string)

' Returns a copy of the given string with each match replaced. sCopy = oRegExp.Replace(string, replacement)

' Returns a MatchCollection object containing matches. Set oMatches = oRegExp.Execute(string)

Code: Example: .Pattern txt="Automation is fast" Set objReg=CreateObject("vbscript.regexp") objReg.Pattern="i" Msgbox objReg.Replace(txt,"##") Out Put: Automat##on is fast

Summary

Regular expression describes one or more strings to match when you search a body of text. The expression serves as a template for matching a character pattern to the string that is being searched.

A regular expression consists of ordinary characters (for example, letters a through z) and special characters, as known metacharacters.

Exercise

Documento similar