Study Notes c # - Regular Expressions (RegularExpression)

First, what is a regular expression?
English Regular Expression, is an important concept in computer science, she uses a mathematical algorithm to solve a computer program text search, matching and other issues,

Regular expression language is a language designed for string manipulation. In many languages provide support for it, c # is no exception, it can help us solve the following problems:
1. Search: regular expressions, we want to get part from a string

2, matching: determining whether a given string matching the filter logic regex

You can think of regular expressions voiced a string of writing rules

Password entered by the user to determine the legality of the user input to determine the legality of the mailbox format

Second, the composition of the regular expression

Regular expression is a common character and special characters (metacharacters become) Text Mode composed.

The model describes one or more strings to be matched when searching the body of text.

Third, the basic syntax metacharacters

Character Description

  . Matches any character except newline

\ W matching letters, numbers, underscore characters (case word refers to the field, 0-9, underlined)

\ W \ w of complement (except "uppercase and lowercase letters, numbers 0-9, underscore" outside)

\ S matches any white space characters (including newline / n, carriage return / r, tabs / t, vertical tab / v, formfeed / f)

\ S \ s complement of (a character other than the s defined)

\ D match numbers (0-9)

\ D d represents the complement addition 0-9)   

 

In the regular expression, \ is the escape character. * Is a metacharacter If you want to represent a \ * character, then, need to use \\ \ *.

 

Fourth, the repeated description string
Character Description
{n} n times to match the previous character
{n,} Matches the preceding character n times or more than n times
{n, m} Matches the preceding character n to m times
? Repeated ZeroOrOne
+ repeated one or more times
* repeated zero or more times

Example: checking the input content is a legal QQ number (Note: QQ No. 5-12 digits)
String isQq1 = "1233", isQq2 = "a1233", isQq3 = "o23456789123",

String regexQq = @ "^ \ D {. 5 , 12 is $}" // input only 5-12 digits
Console.WriteLine (isQq1 + "is a legal QQ number (5-12 digits):" + Regex.lsMatch ( isQq1, regexQq));
Console.WriteLine (isQq2 + "is a legal QQ number (5-12 digits):" + Regex.IsMatch (isQq2, regexQq));
Console.WriteLine (isQq3 + "is a legal QQ No. (5-12 digits): "+ Regex.lsMatch (isQq3, regexQq));

Fifth, choose a matching character

Character Description
| two matching condition logical "or" (or) operation.

 

Six, Regex

Regex class

Regex class for representing a regular expression.

The following table lists some common Regex class methods:

序号 方法 & 描述
1 public bool IsMatch( string input )
指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项。
2 public bool IsMatch( string input, int startat )
指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项,从字符串中指定的开始位置开始。
3 public static bool IsMatch( string input, string pattern )
指示指定的正则表达式是否在指定的输入字符串中找到匹配项。
4 public MatchCollection Matches( string input )
在指定的输入字符串中搜索正则表达式的所有匹配项。
5 public string Replace( string input, string replacement )
在指定的输入字符串中,把所有匹配正则表达式模式的所有匹配的字符串替换为指定的替换字符串。
6 public string[] Split( string input )
把输入字符串分割为子字符串数组,根据在 Regex 构造函数中指定的正则表达式模式定义的位置进行分割。

Seven groups of regular expressions

Example: P4 verify address (such as: 192.168.1.4, is four segments up to three, the maximum number of each segment 255, and the first bit is not 0)

string regexStrIP4=@"^(((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[10]?\d\d?))$";

Console.WriteLine ( "Please enter a IP4 Address:");
String inputStrlp4 Console.ReadLine = ();

Console.WriteLine (inputStrlp4 + "is a legal IP4 address:" + Regex.IsMatch (inputStrlp4, regexStrIP4));

Console.WriteLine ( "Please enter a IP4 address:");

string inputStrip4Second = Console.ReadLine();

Console.WriteLine (regexStrIP4 + "whether it is a legitimate IP4 address:" + Regex.IsMatch (inputStrip4Second, regexStrIP4));

 

 

The IP: 0-255.0-255.0-255.0-255
2 0-4 0-9 first case
250-5 second case
0-10-90-9 third case

@ "At the beginning ^

( (
(2
[0-4] \ D first case    
|
 25 [0-5] The second case
|
?? [01] \ D \ D) third case
. \ Represents a point
)
{3} digit + repeated three times

(2 [0-4] \ d | 25 [0-5] |?? [01] \ d \ d) the last digit 

$ End

 ";

 

 

Guess you like

Origin www.cnblogs.com/AmbitionBoy/p/12008484.html