c#总结几种正则表达式使用

1.提取SQL语句的参数
     List<string> result = new List<string>();
            Regex paramReg = new Regex(@"(?<!@)[^w$#@]@(?!@)[w$#@]+");
            MatchCollection matches = paramReg.Matches(sql);
            foreach (Match m in matches)
            {
                result.Add(m.Groups[0].Value.Substring(m.Groups[0].Value.IndexOf("@")));
            }

2.多个空格合并成一个

(1)result = Regex.Replace(str, "\s{2,}", " ");

(2) Regex replaceSpace = new Regex(@"s{1,}", RegexOptions.IgnoreCase);

  result = replaceSpace.Replace(str, " ").Trim();

3.提取括号中的值

   version = Regex.Replace(str, @"(.*()(.*)().*)", "$2"); //小括号()
    string sheetData = Regex.Match(LinkData, @"{(.*)}", RegexOptions.Singleline).Groups[1].Value;//大括号{}          Regex.Matches(@"ab[wwwe]sadf[www.bai]sadf", @"[(.+?)]");//中括号

   包括小括号:

  @"([^()]*?)";

 @"(.*?)"

4.提取中文信息的表达式。

Regex rx = new Regex("[u4e00-u9fa5]+");

5.提取双引号之间的信息的表达式。

Regex rx = new Regex(""[^"]*"");

Regex rx = new Regex("".*?"");

6.尖括号(包括)

<([^<>]*)>

7.方括号[]不带括号

(?<=[)[^]]*[^]]

原文地址:https://www.cnblogs.com/jinyu20180311/p/13034614.html