Regular Expression 01, Regular Expression
Introduction: The basic syntax of regular expressions and some simple usage methods are used to extract some mailboxes through regular expressions.
I. Introduction to regular expression syntax
Definition: it is a string used for matching, extraction, and replacement that contains metacharacters. It is a technology used for text processing and has nothing to do with language. In fact, a regular expression is actually a template that matches a character pattern with the searched string.
*: Metacharacters
.: Match any single character except \ n. <One. Only one character can be matched>
A. B Indicates acb and adb.
[]: Match any character in brackets <range, number set>
A [1-9] B Represents a1b and a5b.
|: Indicates or
(): Change the priority and group
*: Qualifier
{N}: indicates that the previous expression must appear n times.
A [abcd] {3} B: indicates that [abcd] can appear three times.
{N ,}: indicates that the previous expression appears at least n times.
{N, m}: indicates that at least n times are displayed, and at most m times are displayed.
*: 0 or multiple times.
+: Indicates one or more occurrences.
? : Indicates 0 or 1 times, or this can be used with a qualifier to remove greedy mode.
*: Metacharacters
^: Indicates the start of the regular expression. It also indicates taking non-
^ AB: the matching string must start with AB.
A [^ 0-9] B: represents all the characters except 0-9 between AB.
$: Indicates the end of the regular expression.
Cc $: the matching string must end with cc.
String s = Regex. Escape (@ "\ d {5, 7 }");
Ii. Greedy mode and non-Greedy Mode
Condition for occurrence of greedy mode and non-Greedy mode: When a qualifier is added to the expression, there may be many matching conditions.
Greedy:. + (the default mode is greedy, and as many matches as possible)
Non-Greedy:. +? (Matching as few as possible)
The greedy mode and non-Greedy mode are used to match strings as much as possible.
Iii. Instances
IsMatch (): indicates that if any part of the entire string can be matched, the expression returns true.
Eg1:
While (true) {Console. writeLine ("Enter:"); string write = Console. readLine (); // here the IsMatch is partial match, bool B = Regex. isMatch (write, "[0-9] {6}"); Console. writeLine (B); Console. readKey ();}
Eg2: the user is required to enter an integer to match an array of> = 10 or <= 20.
while (true) { Console.WriteLine("qingshur:"); string msg = Console.ReadLine(); bool b = Regex.IsMatch(msg,"^(1[0-9]|20)$"); Console.WriteLine(b); }
Eg3: determines whether a string is an ID card number. 1: The length is a string of 15 or 18 characters, and the first digit cannot be 0. 2: If the value is 15, all strings are numbers. 3: If the value is 18 characters, the first 17 digits are numbers, the last digit may be a number or X;
While (true) {Console. WriteLine ("Enter your ID number. "); String msg = Console. readLine (); bool B = Regex. isMatch (msg, @ "^ ([1-9] [0-9] {14} | [1-9] [0-9] {16} [0-9xX]) $ "); Console. writeLine (B );}
Eg4: use regular expressions to determine the phone number
While (true) {console. writeline ("Enter your phone number? "); String msg = console. readline (); bool n = regex. ismatch (msg," ^ ([0-9] {3, 4 }-? [0-9] {} | [] {5}) $ "); console. writeline (n );}
Eg5: email matching
^[0-9a-zA-Z_.\-]+@[a-zA-Z\-0-9]+(\.[a-zA-Z]+){1,2}$
Eg6: IP address matching
^[0-9]{3}\.[0-9]{3}\.[0-9]{3}\.[0-9]{3}$
Eg7: Date matching
^[0-9]{4}\-(0[0-9]|1[0-2])?\-(0[0-9]|1[0-2])?$
Eg8: greedy mode 01
String msg = "111.1111.11.11"; // Match with multiple greedy modes (default) Match n = Regex. match (msg ,". + "); // match with fewer Non-Greedy pattern (after the qualifier ?, Disable greedy mode) Match n = Regex. Match (msg, ". +? "); Console. WriteLine (n. Value); Console. ReadKey ();
Eg9: greedy mode 02
String msg = "111. 1111. 11. 11. "; // Greedy match, show all. Match all the preceding operations ,. Let all the previous matches come up with a full stop, so that the following amount is matched. Match n = Regex. Match (msg, ". +. "); // The first four digits are displayed in greedy mode. Match n = Regex. Match (msg, ". + ?. "); Console. WriteLine (n. Value); Console. ReadKey ();
Eg10: Obtain the email address on the webpage through WebClient and regular expression.
WebClient client = new WebClient (); // WebClient encapsulates the request address class // download the webpage content string msg = client. downloadString ("http://blog.sina.com.cn/s/blog_515617e60101e151.html"); MatchCollection matchCollection = Regex. matches (msg, @ "[-a-zA-Z0-9 _.] + @ [a-zA-Z-0-9] + (\. [a-zA-Z] +) {1, 2} "); foreach (Match item in matchCollection) {Console. writeLine (item. value);} Console. writeLine ("{0} mailboxes", matchCollection. count); Console. readKey ();
Attachment:
Regular Expression reference: http://www.cnblogs.com/jamesping/articles/2252675.html