Asp. NET using regular expressions to mask spam _ practical Tips

Source: Internet
Author: User
Tags instance method expression engine
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);
}
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.