字符串操作与正则表达式

Regex类

Regex 类表示不可变(只读)正则表达式类。它还包含各种静态方法,允许在不显式创建其他类的实例的情况下使用其他正则表达式类。Regex 类在System.Text.RegularExpressions命名空间下。 http://www.cnblogs.com/chixiaojin/archive/2011/09/12/2174149.html   匹配字符集

1、这里介绍IsMatch方法。
IsMatch方法:正则表达式在输入字符串中是否找到匹配项。
该方法有四种重载的形式:
public bool IsMatch(string str); 
     表示在构造函数中指定的正则表达式在str中是否找到匹配项。 
public bool IsMatch(string str, int start);
   表示在构造函数中指定的正则表达式在str的指定起始位置开始是否找到匹配项。参数start表示开始搜索的字符位置。
public static bool IsMatch(string str, string pattern);
    表示使用pattern参数中指定的正则表达式是否在str中找到匹配项。
public static bool IsMatch(string str, string pattern, RegexOptions options);
    表示使用pattern参数中指定的正则表达式和options枚举提供的匹配选项在input中是否找到匹配项。其中options是RegexOption枚举值的按位“或”组合。

Regex r = new Regex(@"[0-9a-z]{3,5}");
            string[] tests = {"abc", "123456", "(aa22bb33)", "ab"};
            foreach (string test in tests)
            {
                if (r.IsMatch(test))
                {
                    Console.WriteLine("{0}中有匹配的项", test);
                }
                else
                {
                    Console.WriteLine("{0}中没有匹配的项", test);
                }
            }

2、Match方法

Match类表示单个正则表达式匹配操作的结果,得到的结果是只读的。该类没有公共构造函数,而是用Regex对象的Match方法返回的结果创建该类的对象。

 Regex r = new Regex("abc");
     Match m = r.Match("123abc456");
     if (m.Success)
     { 
         Console.WriteLine("找到匹配位置:" + m.Index);
         Console.WriteLine("找到匹配结果:" + m.Value);
     }

运行结果:
找到匹配位置:3
找到匹配结果:abc

3、MatchCollection类

MatchCollection类表示成功的非重叠匹配的序列,得到的集合是只读的。该类同样没有公共构造函数,而是用Regex.Matches方法返回的结果创建该类的对象。

 Regex r = new Regex("abc");
    MatchCollection mc = r.Matches("123abc4abcd");
    int count = mc.Count;
    String[] results = new String[count];
    int[] matchPosition = new int[count];
    for (int i = 0; i < count; i++)
    {
         results[i] = mc[i].Value;
         matchPosition[i] = mc[i].Index;
         Console.WriteLine("第{0}个匹配结果:{1},位置:{2}",i+1, results[i],      matchPosition[i]);
    }

运行结果:
第1个匹配结果:abc,位置:3
第2个匹配结果:abc,位置:7

public string sb = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            sb = "123456;http://img999.taobaocdn.com/imgextra/i3/37466590/T2IqYnXXpaXXXXXXXX_!!37466590.jpg&quot; width=&quot;750&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 36.0px;&quot;&gt;&lt;span style=&quot;font-family: microsoft yahei;&quot;&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&lt;a title=&quot;&quot; href=&quot;http://item.taobao.com/item.htm?spm=686.1000925.1000774.6.2UW53Z&id=21196964907&quot; target=&quot;_self&quot;&gt;&lt;/a&gt;预售2013年新款蕾丝打底衫超美,预计2013年2月底3月初发货,5折包邮首发 先买先发!&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 36.0px;color: #ff0000;font-family: microsoft yahei;&quot;&gt;限量第一批500件109&lt;/span&gt;&lt;span style=&quot;font-size: 36.0px;&quot;&gt;&lt;span style=&quot;font-family: microsoft yahei;&quot;&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;元包邮!&lt;span style=&quot;font-size: 36.0px;&quot;&gt;&lt;span style=&quot;font-family: microsoft yahei;&quot;&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;限量第二批500件129元不包邮&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;http://img04.taobaocdn.com/imgextra/i4/37466590/T2CFGVXgNbXXXXXXXX_!!37466590.jpg&quot; align=&quot;absMiddle&quot;&gt;&lt;img src=&quot;http://img04.taobaocdn.com/imgextra/i4/37466590/T25Vm8XnBXXXXXXXXX_!!37466590.jpg&quot; align=&quot;absMiddle&quot;&gt;&lt;img src=&quot;http://img01.taobaocdn.com/imgextra/i1/37466590/T2P_GsXidcXXXXXXXX_!!37466590.jpg&quot; align=&quot;absMiddle&quot;&gt;&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;http://img02.taobaocdn.com/imgextra/i2/37466590/T2xCXdXotNXXXXXXXX_!!37466590.jpg&quot; align=&quot;absMiddle&quot;&gt;&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;http://img01.taobaocdn.com/imgextra/i1/37466590/T2u2DwXcxXXXXXXXXX_!!37466590.jpg&quot; align=&quot;absMiddle&quot;&gt;&lt;img src=&quot;http://img03.taobaocdn.com/imgextra/i3/37466590/T2hLnwXfpXXXXXXXXX_!!37466590.jpg&quot; align=&quot;absMiddle&quot;&gt;&lt;/p&gt;";

            Regex r = new Regex("http://img" + "[0-9]{2}" + ".taobaocdn.com" + "[a-zA-Z0-9/_!]{40,60}" + ".jpg");
            MatchCollection mc = r.Matches(sb);
            int count = mc.Count;
            String[] results = new String[count];
            int[] matchPosition = new int[count];
            for (int i = 0; i < count; i++)
            {
                results[i] = mc[i].Value;
                matchPosition[i] = mc[i].Index;
                Response.Write(""+i+"个匹配结果:"+results[i]+",位置:"+matchPosition[i]+"</br>");
            }
        }

显示结果:

第0个匹配结果:http://img04.taobaocdn.com/imgextra/i4/37466590/T2CFGVXgNbXXXXXXXX_!!37466590.jpg,位置:1131
第1个匹配结果:http://img04.taobaocdn.com/imgextra/i4/37466590/T25Vm8XnBXXXXXXXXX_!!37466590.jpg,位置:1268
第2个匹配结果:http://img01.taobaocdn.com/imgextra/i1/37466590/T2P_GsXidcXXXXXXXX_!!37466590.jpg,位置:1405
第3个匹配结果:http://img02.taobaocdn.com/imgextra/i2/37466590/T2xCXdXotNXXXXXXXX_!!37466590.jpg,位置:1561
第4个匹配结果:http://img01.taobaocdn.com/imgextra/i1/37466590/T2u2DwXcxXXXXXXXXX_!!37466590.jpg,位置:1717
第5个匹配结果:http://img03.taobaocdn.com/imgextra/i3/37466590/T2hLnwXfpXXXXXXXXX_!!37466590.jpg,位置:1854

4、Group类

Group类表示单个捕获组的结果。当与正则表达式匹配的子字符串有多组时,可以使用该类得到某一组的结果。

  string text = "One car red car blue car";
              string pat = @"(w+)s+(car)";
             Regex r = new Regex(pat, RegexOptions.IgnoreCase);
             Match m = r.Match(text);
             int matchCount = 0;
             while (m.Success)
             {
                  Console.WriteLine("Match" + (++matchCount));
                  for (int i = 1; i <= 2; i++)
                  {
                     Group g = m.Groups[i];
                     Console.WriteLine(string.Format("Group{0}='{1}'", i, g));
                     CaptureCollection cc = g.Captures;
        for (int j = 0; j < cc.Count; j++)
                {
                    Capture c = cc[j];
                    Console.WriteLine(string.Format(
                         "Capture{0}='{1}', Position={2}", j, c, c.Index));
                }
            }
            m = m.NextMatch();
        }
        Console.ReadLine();

输出结果:
Match1
Group1='One'
Capture0='One', Position=0
Group2='car'
Capture0='car', Position=4
Match2
Group1='red'
Capture0='red', Position=8
Group2='car'
Capture0='car', Position=12
Match3
Group1='blue'
Capture0='blue', Position=16
Group2='car'
Capture0='car', Position=21

原文地址:https://www.cnblogs.com/chixiaojin/p/2979528.html