C # Learning Log Day 6------Common Regular Expression examples

Source: Internet
Author: User

C # supports regular expression matching text, where the use of regular expressions is discussed, and examples of matching regular expressions are used.

1. Use regular expressions in VS

Using a regular expression in VS requires a using-System.Text.RegularExpressions namespace.

The namespace includes the following classes

Capture Capturing results for a single expression
CaptureCollection For a sequence of string captures
Group Represents the result of a single capture
GroupCollection Represents a rally for a capturing group
Match Represents the result of matching a single regular expression
MatchCollection Represents the application of a regular expression to a string in an iterative way
Regex A regular expression that represents an immutable
RegexCompilationInfo The compiled regular expression needs to provide information

which

Constructing regular expressions involves the Regex class, which includes: IsMatch (), replace (), split (), and other method methods in the Regex class, where the IsMatch (), replace (), split () methods are described

  

(1) IsMatch () method;

The IsMatch () method is actually a return bool-worthy method, if the test character satisfies the regular expression return true otherwise returns false;

Example: Regex reg = new Regex ("022\\d{8}");//Constructs a phone number that starts with 022

String temp = "02222332332";

String temp1 = "02322332332";

Console.WriteLine (Reg. IsMatch (temp). ToString ());

Console.WriteLine (Reg. IsMatch (TEMP1). ToString ());

The result is True

False

(2) Replace () method;

The replace () method is actually a replacement method, replacing a matching regular expression matching pattern;

Example: replace @ In a mailbox and turn it into an at

Regex reg = new Regex ("\w{1,}@\w{1,}\."); /e-mail address

string email = "[email protected]";

Console.WriteLine (Reg. Replace (email, "@", "at");

Output is: whateveratqq.com

(3) Split () method;

The split () method is actually a split method, which is stored in a string array based on a matching regular expression;

Example: separating text with spaces and semicolons as separators

Regex reg = new Regex ("\s|;");

string mes = "Hello Nihao;zaijian";

String[] temp;

temp = Reg. Split (temp);

foreach (string str in temp) {

Console.WriteLine (str. ToString ());

}

Result: Hello

Nihao

Zaijian

2 The basic syntax for some C # regular rules is described below

Character matching syntax:

character Syntax Grammatical explanations Syntax Examples
\d Matching numbers (0~9) ' \d ' matches 8, does not match 12;
\d Match non-numeric ' \d ' matches C, does not match 3;
\w Match any single character ' \w\w ' matches A3, does not match @3;
\w Match Non-single character ' \w ' matches @, does not match C;
\s Match white space characters ' \d\s\d ' matches 3 D, does not match ABC;
\s Match non-null characters ' \s\s\s ' matches a#4, mismatch 3 D;
. Match any character ' ... ' matches a$ 5, does not match line breaks;
[...] Match any character in parentheses [b-d] matches B, C, D, does not match E;
[^ ...] Match non-bracket characters [^b-z] matches a, does not match the character of B-z;

Repeating match syntax:

repeating Syntax Grammatical explanations Syntax Examples
N Match N-Times characters \d{3} matches \d\d\d, does not match \d\d or \d\d\d\d
{N,} Matches n times and more than n times \W{2} matches \w\w and \w\w\w above and does not match \w
{N,m} Match n times on M times down \s{1,3} matches \s,\s\s,\s\s\s, mismatch \s\s\s\s
? Match 0 or 1 times 5. Match 5 or 0, mismatch not 5 and 0
+ Match one or more times \s+ matches more than one \s, does not match more than one \s
* Match more than 0 times \w* matches more than 0 \w, does not match non-n*\w

Character positioning Syntax:

repeating Syntax Grammatical explanations Syntax Examples
^ Locate the back mode start position
$ The front pattern is at the end of the string
\a Front mode start position
\z Previous mode End position
\z Previous mode end position (before line break)
\b Match a word boundary
\b Match a non-word boundary

Escape matching syntax:

Escape Syntax characters involved (syntax explanation) Syntax Examples
"\" + Actual character \ . * + ? | ( ) { }^ $ For example: \ \ matches the character "\"
\ n Match line break
\ r Match carriage return
\ t Match Horizontal Tabs
\v Match vertical Tabs
\f Match a page break
\nnn Match a 8 binary ASCII
\xnn Match a 16 binary ASCII
\unnnn Match 4 x 16 binary Uniode
\c+ Capital Letters Match Ctrl-Caps Example: \cs-matching Ctrl+s

3, the following are some common regular matching statements:

1. Verify the user name and password: ("^[a-za-z]\w{5,15}$") the correct format: "[a-z][a-z]_[0-9]" composition, and the first word must be a letter 6~16 bit;

2. Verify the phone number: ("^ (\d{3.4}-) \d{7,8}$") correct format: xxx/xxxx-xxxxxxx/xxxxxxxx;

3. Verify the ID number (15-bit or 18-digit number): ("^\d{15}|\d{18}$");

4. Verify the email Address: ("^\w+ [-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$ ");

5. Only a string consisting of a number and 26 English letters can be entered: ("^[a-za-z0-9]+$");

6. Integer or decimal: ^[0-9]+\. {0,1} [0-9] {0,2}$

7. Enter only the number: "^[0-9]*$".

8. Only n digits can be entered: "^\d{n}$".

9. You can only enter numbers with at least n digits: "^\d{n,}$".

10. Enter only the digits of the m~n bit:. "^\d{m,n}$"

11. Enter only numbers that start with 0 and not 0: "^ (0|[ 1-9][0-9]*) $ ".

12. Only positive real numbers with two decimal places can be entered: "^[0-9]+ (. [ 0-9]{2})? $ ".

13. You can only enter positive real numbers with a decimal position: "^[0-9]+ (. [ 0-9]{1,3})? $ ".

14. Only non-zero positive integers can be entered: "^\+?" [1-9] [0-9]*$].

15. You can only enter a non-zero negative integer: "^\-[1-9][]0-9" *$.

16. Only enter a character with a length of 3: "^. {3}$ ".

17. You can only enter a string consisting of 26 English letters: "^[a-za-z]+$".

18. You can only enter a string consisting of 26 uppercase English letters: "^[a-z]+$".

19. You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$".

20. Verify that there are ^%& ',; =?$\ ' characters: ' [^%& ',; =?$\x22]+ '.

21. Only Chinese characters can be entered: "^[\u4e00-\u9fa5]{0,}$"

22. Verify the URL: "^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? $ ".

23. Verification 12 months of the year: "^ (0?[ 1-9]|1[0-2]) $ "The correct format is:" 01 "~" 09 "and" 1 "~" 12 ".

24. Verify one months of 31 days: "^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "correct format for;" 01 "~" 09 "and" 1 "~" 31 ".

C # Learning Log Day 6------Common Regular Expression examples

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.