C # common Regular Expressions

Source: Internet
Author: User
Tags expression engine

The Regular Expression in. NET is in the System. Text. RegularExpressions namespace, which provides various powerful functions of regular expressions.

Many classes are used: Group, GroupCollection, Match, MatchCollection, and Regex.

RegexClass indicates an unchangeable regular expression.

RegexClass contains severalStaticMethods, these methods andRegexThe functions of object methods are the same. In. NET Framework 2.0, they are slightly different. In. NET2.0, SetCacheA regular expression compiled by calling a static methodNo CacheThe regular expression compiled by calling the instance method. By default, the Regular Expression Engine caches 15 recently used static regular expressions. Therefore, static methods are recommended when a set of regular expressions are frequently used.

Note: You can set static attributesRegex. CacheSizeTo dynamically change the maximum number of items in the current static cache of compiled regular expressions.

 

ForCharacter class. List only a few common ones.

Character class Description
[Character grouping] Matches any character in the specified character group.
[^ Character group] Match any character that is not in the specified character group.
[First character-last character] Match any character in the character range.
\ W Match any word character. It is equivalent to [a-zA-Z_0-9]. "_" Indicates the character.
\ W Matches any non-word character. Equivalent to [^ a-zA-Z_0-9].
\ S Matches any blank character. It is equivalent to [\ f \ n \ r \ t \ v]. Note that there is a space before "\ f.
\ S Matches any non-blank characters. It is equivalent to [^ \ f \ n \ r \ t \ v].
\ D Matches any decimal number. It is equivalent to [0-9].
\ D Matches any non-numeric character. It is equivalent to [^ 0-9].

Next we will introduceRegexCommon methods and attributes of a class.

  • Method
Name Description
IsMatch Overloaded. Indicates whether the regular expression matches the input string.
Match Overloaded. Search for matching items of the regular expression in the input string, and return the first exact result as a single Match object.
Matches Overloaded. Search for all the matching items of the regular expression in the input string and return all successful matches, just like calling Match multiple times.
Replace Overloaded. Within the specified input string, use the specified replacement string to replace the string that matches a regular expression pattern.
Split Overloaded. Splits the input string into a substring array at the position defined by the regular expression match.
  • Attribute
Name Description
CacheSize Gets or sets the maximum number of items in the current static cache for compiled regular expressions.
Options Returns the input Regex constructor option.
RightToLeft Gets a value that indicates whether the regular expression searches from right to left.

Options is the RegexOptions enumeration, which provides Options for setting regular expressions.

Note that this option is not only one option, but can be implemented using the bitwise OR symbol "|", for example, RegexOptions. compiled | RegexOptions. ignoreCase requires that the regular expression be compiled into an assembly and case-insensitive.

The following describes how to use various methods through examples.

  • Regex. IsMatch Method -- this method is used when you only want to verify whether the format is correct

 

Common Regular Expressions

Non-negative integer (positive integer)

Positive Integer

Non-positive integer (negative integer)

Negative integer

Integer

Non-negative floating point number (Positive floating point number)

Positive floating point number

Non-Positive floating point number (negative floating point number)

Negative floating point number

Floating Point Number

A string consisting of letters.

A string consisting of uppercase letters.

A string consisting of lowercase letters.

A string consisting of digits and letters

A string consisting of digits, letters, or underscores.

Address

  

Only numbers can be entered: ""

Only a digit can be entered: ""

You can only enter at least a digit: ""

Only a digit can be entered: ""

Only numbers starting with zero and non-zero can be entered: ""

You can only enter a positive number with two decimal places: ""

Only positive numbers with decimal places can be entered: ""

Only a non-zero positive integer can be entered: ""

You can only enter a non-zero negative integer: ""

Only characters in length can be entered: ""

You can only enter a string consisting of letters: ""

Only a string consisting of uppercase letters can be entered: ""

Only a string consisting of lowercase English letters can be entered: ""

You can only enter a string consisting of numbers and letters: ""

You can only enter a string consisting of digits, letters, or underscores (_): ""

Verify that the user password "" is in the correct format: starts with a letter, length,

It can only contain characters, numbers, and underscores.

Verify whether the file contains the following characters: ""

Only Chinese characters can be entered: ""

Verification address: ""

Verification: ""

Verification phone number: ""

The correct format is: "",

"", "", "".

Verify ID number (digit or digit): ""

Verification year month: "" is in the correct format: "and"

Verify the day of the month: ""

The correct format is "and".

 

 

 

 

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.