Regular expression rules summary

The main sources: https://www.cnblogs.com/caokai520/p/4511848.html

Recommended to walk through video, then look at the document, I did despise the text is too long, been on and off the look, but after I read C # basic video, in fact, not much. Free online video will explain the basis, the foundation read, the text does not look tired. Find unmatched knowledge points did not add, you have to make up time.

This Article

     Regular expressions essence is to use a series of special character mode, to represent a certain type of string. Regular expressions is undoubtedly the most powerful text processing tools, and .NET Regex class implementation provided a method for verifying the regular expression. Regex class represents immutable (read-only) regular expression. It also contains a variety of static methods, the use of other regular expression classes allow the case to create additional instances of the class without explicit.

Comb foundation

Description:

Since the regular expression "\",, "*", "^", "$", "+", "(", ")", "?" "|", "{", "[", Etc. special character has a certain significance, if required by their original meaning, it should be escaped, for example, in the hope that at least one string, "\", then the regular expression would look like this: \\ +.

For example digital

            @即我们不需要对转义字符加上 \ (反斜扛)
            // 以数字开头,以数字结尾
            Regex reg = new Regex(@"^[0-9]*$");
            //n位的数字{n}匹配n次
            Regex reg = new Regex(@"^\d{n}$");
            //至少n位的数字{n,}
            Regex reg = new Regex(@"^\d{n,}$");
            //m-n位的数字{m,n}
            Regex reg = new Regex(@"^\d{m,n}$");
            //零和非零开头的数字
            Regex reg = new Regex(@"^(0|[1-9][0-9]*)$");
            //非零开头的最多带两位小数的数字+匹配前面的一次或多次的子表达式
            // ?匹配前面的或0次或一次的子表达式
            Regex reg = new Regex(@"^([1-9][0-9]*)+(.[0-9]{1,2})?$");
            //带1-2位小数的正数或负数
            Regex reg = new Regex(@"^(\-)?\d+(\.\d{1,2})?$");
            //正数、负数、和小数(\-|\+)(负数或者正数)
            Regex reg = new Regex(@"^(\-|\+)?\d+(\.\d+)?$");
            //有两位小数的正实数
            Regex reg = new Regex(@"^[0-9]+(.[0-9]{2})?$");
            //有1~3位小数的正实数
            Regex reg = new Regex(@"^[0-9]+(.[0-9]{1,3})?$");
            //非零的正整数*匹配前面2次或多次的
            Regex reg = new Regex(@"^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$");
           

(Exp) grouping

 

When do we often get A reptile of some useful information. As href, title, and content display.

Using regular expressions () contains the text automatically will be named as a group.

Description: Antisense metacharacter corresponding meta characters can be combined to match any character. Such as: [\ w \ W], [\ s \ S], [\ d \ D] ..

string TaobaoLink = "<a href=\"http://www.taobao.com\" title=\"淘宝网 - 淘!我喜欢\" target=\"_blank\">淘宝</a>";
RegexStr = @"<a[^>]+href=""(\S+)""[^>]+title=""([\s\S]+?)""[^>]+>(\S+)</a>";
Match mat = Regex.Match(TaobaoLink, RegexStr);
for (int i = 0; i < mat.Groups.Count; i++)
{
    Console.WriteLine("第"+i+"组:"+mat.Groups[i].Value);
}

(? <name> exp)  Packet name

    Packet can be easily named by (? <Name> exp). And then acquires the packet values ​​Groups [ "name"].

当我们匹配分组信息过多后,在某种场合只需取当中某几组信息。这时我们可以对分组取名。通过分组名称来快速提取对应信息。

string Resume = "基本信息姓名:CK|求职意向:.NET软件工程师|性别:男|学历:本专|出生日期:1988-08-08|户籍:湖北.孝感|E - Mail:[email protected]|手机:15000000000";
RegexStr = @"姓名:(?<name>[\S]+)\|\S+性别:(?<sex>[\S]{1})\|学历:(?<xueli>[\S]{1,10})\|出生日期:(?<Birth>[\S]{10})\|[\s\S]+手机:(?<phone>[\d]{11})";
Match matc = Regex.Match(Resume, RegexStr);
Console.WriteLine("姓名:{0},手机号:{1}", matc.Groups["name"].ToString(), matc.Groups["phone"].ToString());

Guess you like

Origin blog.csdn.net/ab31ab/article/details/90486262