当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET使用正则表达式屏蔽垃圾信息

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

2018年04月20日  | 移动技术网IT编程  | 我要评论

桓楚 余英,idoido国语版,华夏网校

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);
}
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网