您的位置:首页 > 编程语言 > ASP

ASP.NET使用正则表达式屏蔽垃圾信息

2008-09-26 00:00 465 查看
Regex 类
表示不可变的正则表达式。
命名空间:System.Text.RegularExpressions
Regex 类包含若干 static(在 Visual Basic 中为 Shared)方法,使您无需显式创建 Regex 对象即可使用正
则表达式。在 .NET Framework 2.0 版中,将缓存通过调用静态方法而编译的正则表达式,而不会缓存通过调
用实例方法而编译的正则表达式。默认情况下,正则表达式引擎将缓存 15 个最近使用的静态正则表达式。因
此,在过度地依赖一组固定的正则表达式来提取、修改或验证文本的应用程序中,您可能更愿意调用这些静态
方法,而不是其相应的实例方法。IsMatch、Match、Matches、Replace 和 Split 方法的静态重载可用。
using System; 
using System.Text.RegularExpressions; 
public class Test 
{ 
public static void Main () 
{ 
// Define a regular expression for currency values. 
Regex rx = new Regex(@"^-?\d+(\.\d{2})?$"); 
// Define some test strings. 
string[] tests = {"-42", "19.99", "0.001", "100 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); 
} 
} 
} 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: