C # Regular Expression guide-character group

Source: Internet
Author: User

Character group: various characters that may appear in the same seat.

Use a regular expression to determine numeric characters:

Re. Search ("[0123456789]", charStr )! = None

[0123456789] is a regular expression in the form of a string. It is a character group and can be any character ranging from 0 to 9.
Net Regex. IsMatch (charStr, "[0123456789]");
By default, Search (Pattern, String) only determines whether a substring can match pattern. If pattern can match a part of String, it is considered that the match is successful, to test whether the entire String matches pattern, add ^ and $ at both ends of pattern. they indicate the start and end positions of the positioning String. This ensures that only the entire String can be matched by pattern.
For a character group like [0123456789], you can also use the range Notation: [0-9]

In a character group, "-" indicates the range. Generally, it is based on the corresponding code value of the character. The smaller the code value is before "-", and the larger one is behind.
In the above example, "-" is used to indicate the range and cannot match the X-ray character. This type of character is called metacharacters. For example, [,], ^, and $ are all metacharacters.

When we need to match these special metacharacters, we need to escape them.
Like the "-" character, if it is next to "[", it will be considered a common character. In other cases, it is a metacharacter. You can use "\" to escape the metacharacters:
Re. Search ("^ [0 \-9] $", "3 ")! = None // false
The above "\" character itself will be used together with other such characters as "\ n \ r". For separate use, "\" is also required for escape.
Use the native string: re. Search (r "^ [0 \-9] $", "3 ")! = None. Add r to the front of the string. "\" Is not used to represent.
Excluded character group: [^...]: indicates the current position, matching a character not listed.
[^ 0-9]: Indicates matching a character that is not a number.
Character group Notation:
Common examples include:
\ D: [0-9]
\ W: [0-9a-zA-Z] This also contains an underline
\ S: [\ t \ r \ n \ v \ f]

The following is a summary of excluded character groups:
\ D: complementary to \ d
\ W: complementary to \ w
\ S: complementary to \ s
The simplest application: [\ s \ S] is used in combination to match all characters.

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.