• 正则表达式


     用途:

    1. 判断给定的整个字符串是否满足某pattern。//Regex.IsMatch

    2. 在给定的整个字符串中找出满足某pattern的所有字串。给定字符串里是否存在某字串满足某pattern。//Regex.Matchs,Regex.IsMatch

    上述pattern就是正则表达式。

    任何的pattern都要@修饰,比如@"^[^/:*?""<>| ]{1,244}$",@"d{2}-d{5}"。

    用途1)的pattern的首尾要有^,$ e.g. 文件夹名字的正则表达式 @"^[^/:*?""<>| ]{1,244}$"

    用途2)的pattern的首尾不能有^,$。

      string input3 = "adf234-56789sdf123-76589asdf";
      string pattern3 = @"d{2}-d{5}";

      字串34-56789,23-76589都会搜索出来。

    例子分析

    1.

    使用多线模式,其中 ^ 和 $ 匹配每行的开头和末尾,不是输入字符串的开头和末尾。

    默认情况下,$ 仅与输入字符串的末尾匹配。 如果指定了 RegexOptions.Multiline 选项,它将与换行符 ( ) 或输入字符串的末尾匹配。 但是,它并不与回车符/换行符的组合匹配。 若要成功匹配它们,使用子表达式  ?$ 只替代 $。当然最终的match结果不含^,$所匹配的字符。

    下面例子来自http://msdn.microsoft.com/zh-cn/library/vstudio/yd1hzczs.aspx,但是稍作修改,因为windows下换行是由 组成的,所以这里用WriteLine。

    第一个 pattern = @"^(w+)s(d+)$";显然不行。

    $这里要匹配 ,而因为windows下换行是由 组成的,所以pattern里还有要有个 ?,?必须加上,你不能假设换行肯定是由 组成的。

    一个缺点就是匹配的match.Value字符的最后有个 .

     1 static void Test3()
     2         {
     3             StringWriter writer = new StringWriter();
     4             writer.WriteLine("Joe 164");
     5             writer.WriteLine("Sam 208");
     6             writer.WriteLine("Allison 211");
     7             writer.WriteLine("Gwen 171");
     8             
     9             string input = writer.ToString();
    10             string pattern = @"^(w+)s(d+)$";
    11             bool matched = false;
    12 
    13             Console.WriteLine("Without Multiline option:");
    14             foreach (Match match in Regex.Matches(input, pattern))
    15             {
    16                 matched = true;
    17                 break;
    18             }
    19 
    20             if (!matched)
    21                 Console.WriteLine("   No matches.");
    22             Console.WriteLine();
    23 
    24             // Redefine pattern to handle multiple lines.
    25             pattern = @"^(w+)s(d+)
    ?$";
    26             Console.WriteLine("With multiline option:");
    27             foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
    28             {
    29                 Console.WriteLine(match.Value); // print whole the match
    30             }
    31         }

     例2:

    http://zhidao.baidu.com/question/578743567.html?from=pubpage&msgtype=2

    找出输入文本的非空的第一行。

     1         static void Test4()
     2         {
     3             StringWriter writer = new StringWriter();
     4             writer.WriteLine();
     5             writer.WriteLine();
     6             writer.WriteLine();
     7             writer.WriteLine("Joe 164");
     8             writer.WriteLine("Sam 208");
     9             writer.WriteLine("Allison 211");
    10             writer.WriteLine("Gwen 171");
    11 
    12             string input = writer.ToString();
    13 
    14             // Redefine pattern to handle multiple lines.
    15             string pattern = @".+(?=(
    ?
    ))";
    16             Console.WriteLine("With multiline option:");
    17 
    18             Match mat = Regex.Match(input, pattern, RegexOptions.Multiline);
    19             
    20             //foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
    21             //{
    22                 Console.WriteLine(mat.Value); // print whole the match
    23             //}
    24         }

    基本语法,参考文章:

    http://msdn.microsoft.com/zh-cn/library/ae5bf541(VS.80).aspx

    http://msdn.microsoft.com/zh-cn/library/vstudio/yd1hzczs.aspx

  • 相关阅读:
    网络分析(二)定向与非定向
    Flex 找不到html文件,不能自动生成html问题解决
    常用的功能点记录
    git常规操作命令整理
    语境驱动测试7原则
    探索式测试的问与答
    测试建模:Google ACC
    探索式测试:基于测程的测试管理(SessionBased Test Management)
    用Excel展示SQL Server中的数据 (III): IronPython与自动化
    在Ajax中使用Flash实现跨域数据读取
  • 原文地址:https://www.cnblogs.com/dirichlet/p/3247786.html
Copyright © 2020-2023  润新知