Regex class
Represents an immutable regular expression.
Namespaces: System.Text.RegularExpressions
The Regex class contains several static (Shared in Visual Basic) methods that allow you to use a Regex object without having to explicitly create it
is an expression. In the. NET Framework version 2.0, a regular expression compiled by calling a static method is cached without caching the
A regular expression compiled with an instance method. By default, the regular expression engine caches 15 of the most recently used static regular expressions. Because
This, in an application that relies heavily on a fixed set of regular expressions to extract, modify, or validate text, you might prefer to invoke these static
method, rather than its corresponding instance method. Static overloads of the IsMatch, Match, matches, Replace, and Split methods are available.
Copy Code code as follows:
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
//Define a regular expression for currency V Alues.
Regex rx = new Regex (@ "^-?\d+ (\.\d{2})? $");
//Define some test strings.
String[] Tests = {" -42", "19.99", "0.001", "USD",
". 34", "0.34", "1,052.21"};
//Check each test string against the regular expression.
foreach (String test in Tests)
{
if (RX). IsMatch (test)
{
Console.WriteLine ("{0} is a currency value.", test);
}
Else
{
Console.WriteLine ("{0} is not a currency value.", test);
}
}
}
}