|
|
The RegEx Object
The Regex
class is the heart of the Regular Expressions namespace. It represent a regular
expression, and allows you to process strings for matches. You can work with
regular expressions using only this class, but you cannot work with regular
expressions without this class. Therefore, it's important that you have a good
grasp of this class's properties and methods.
The diagram on the right shows the methods and properties of the Regex
class. The methods dealing with Matches, IsMatch(), Match(), and Matches() are
the most frequently used.
Constructor
Since the Regex class represents an immutable regular expression, you have to
pass the regular expression when constructing a Regex object. Note that
instantiating a Regex object is not required, as there are also static methods
for performing regular expression pattern matching.
There are two variants of the Regex constructor that you can use:
Regex(string pattern);
Regex(string pattern, RegexOptions options);
Sub New(pattern as String)
Sub New(pattern as String, options as RegexOptions)
|
|
In both cases, the pattern parameter is the regular expression pattern. The
RegexOptions parameter is an optional parameter through which you can specify
the way the pattern matching algorithm should behave, such as being case
insensitive.
The following code snippet shows the instantiation of a Regular Expression,
which simply searches for the word James, using the two different constructors:
Regex rex1 = new Regex("James");
Regex rex2 = new Regex("James", RegexOptions.IgnoreCase);
Dim rex1 as Regex = new Regex("James")
Dim rex2 as Regex = new Regex("James", RegexOptions.IgnoreCase)
|
|
IsMatch
The IsMatch method has both object and static variants, and indicates whether
at least one match was found. First, we'll look at the case where we
have instantiated a Regex object.
bool IsMatch(string input);
bool IsMatch(string input, int startat);
Function IsMatch(string input) as Boolean
Function IsMatch(input as String, startat as Integer) as Boolean
|
|
The input is the text that you want to apply the pattern against. If you do not
want to test the entire string, you can include a starting position at which to
start the search. Using the Regex object instantiated above, we could test for
a match using this code:
if (rex1.IsMatch("My name is John") == true)
 Console.WriteLine("Found a match.");
if (rex2.IsMatch("My name is James") == true)
 Console.WriteLine("Found a match.");
if (rex1.IsMatch("My name is John") = true) then
 Console.WriteLine("Found a match.")
end if
if (rex2.IsMatch("My name is James") = true) then
 Console.WriteLine("Found a match.")
end if
|
|
Alternatively, you can use the static IsMatch methods, which are the equivalent
of creating a one-time use Regex object. They have the following signatures:
static bool IsMatch(string input, string pattern);
static bool IsMatch(string input, string pattern, RegexOptions options);
Shared Function IsMatch(input as String, pattern as String)
Shared Function IsMatch(input as String, pattern as String, options as RegexOptions)
|
|
Modifying our earlier example to use the static methods would result in:
if (Regex.IsMatch("My name is John", "James") == true)
Console.WriteLine("Found a match.");
if (Regex.IsMatch("My name is james", "James", RegexOptions.IgnoreCase) == true)
Console.WriteLine("Found a match.");
if (Regex.IsMatch("My name is John", "James") = true) then
Console.WriteLine("Found a match.")
end if
if (Regex.IsMatch("My name is james", "James", RegexOptions.IgnoreCase) = true)
Console.WriteLine("Found a match.")
end if
|
|
Replace
The regular expression engine can substitute matches as well as identify them.
There are several variations of the Replace method, from those that handle the
replacement directly to those that rely on a MatchEvaluator delegate that you
implement, to the static vs. instance variations. The static and instance methods
that handle the replacement without using delegates are listed below.
string Replace(string input, string replacement);
string Replace(string input, string replacement, int count);
string Replace(string input, string replacement, int count, int startat);
static string Replace(string input, string pattern, string replacement);
static string Replace(string input, string pattern, string replacement, RegexOptions options);
Function Replace(input as String, replacement as String) as String
Function Replace(input as String, replacement as String, count as Integer) as String
Function Replace(input as String, replacement as String, count as Integer, startat as Integer) as String
Shared Function Replace(input as String, pattern as String, replacement as String) as String
Shared Function Replace(input as String, pattern as String, replacement as String, options as RegexOptions) as String
|
|
For example, suppose that you wanted to make every instance of the term
DotNetCoders in a document a hyperlink to the DotNetCoders homepage. You could
accomplish that using static or instance invocations:
Regex rex1 = new Regex("DotNetCoders");
rex1.Replace("DotNetCoders - .NET Site", "<a href=\"http://www.dotnetcoders.com\">DotNetCoders</a>");
Regex.Replace("DotNetCoders - .NET Site", "DotNetCoders", "<a href=\"http://www.dotnetcoders.com\">DotNetCoders</a>");
Dim rex1 as Regex = new Regex("DotNetCoders")
rex1.Replace("DotNetCoders - .NET Site", "<a href=""http://www.dotnetcoders.com"">DotNetCoders</a>")
Regex.Replace("DotNetCoders - .NET Site", "DotNetCoders", "<a href=""http://www.dotnetcoders.com"">DotNetCoders</a>")
|
|
These examples are rather trivial, and could be accomplished with the Replace()
method on the String object as well. As you learn more about regular expression
syntax, and the ability to use a MatchEvaluator delegate, you'll see that
regular expression replacement is much more powerful.
Split
The Split method, like it's String counterpart, divides a string into an array
of substrings at the regular expression match points. Here are the more common
Split method signatures:
string[] Split(string input);
static string[] Split(string input, string pattern);
static string[] Split(string input, string pattern, RegexOptions options);
Function Split(input as String) as String()
Function Split(input as String, pattern as String) as String()
Shared Function Split(input as String, pattern as String,, options as RegexOptions)
|
|
This example splits the alphabet at the vowels: a,e,i,o,u.
Regex.Split("abcdefghijklmnopqrstuvwxyz", "(a|e|i|o|u)")
Regex.Split("abcdefghijklmnopqrstuvwxyz", "(a|e|i|o|u)")
|
|
This results in an array containing the following elements:
bcd
fgh
jklmn
pqrst
vwxyz
Notice that the matched character is not included in the split, and because out string
began with the letter a, the first value is blank.
ToString
The ToString method is overridden to output the pattern represented by the
Regex object.
Regex rex = new Regex("(a|e|i|o|u)");
Console.Writeline("Expression is : " + rex.ToString());
Dim rex as Regex = new Regex("(a|e|i|o|u)")
Console.Writeline("Expression is : " + rex.ToString())
|
|
|
|