+ -
当前位置:首页 → 问答吧 → 正则 RegexOptions.Multiline

正则 RegexOptions.Multiline

时间:2011-12-06

来源:互联网

RegexOptions.Multiline 
指定了^和$可以匹配行的开头和结尾,也就是说使用了换行分割,每一行能得到不同的匹配
我试了下 用了换行 但是匹配出来的还是一样的  
肯定我弄错了 求一个正确的例子
C# code
            string test = @"A hello world A\n B xiawuhao xiongdi B";
            Regex reg = new Regex("^.*?$");
            Console.WriteLine(reg.Match(test));
            Regex reg1 = new Regex("^.*?$", RegexOptions.Multiline);
            Console.WriteLine(reg1.Match(test));

作者: wtcsy   发布时间: 2011-12-06

RegexOptions.Multiline 
多行模式。更改 ^ 和 $ 的含义,使它们分别在任意一行的行首和行尾匹配,而不仅仅在整个字符串的开头和结尾匹配。

C# code

void Main()
{
    StringBuilder input = new StringBuilder();
            input.AppendLine("These is the first test line");
            input.AppendLine("These is the second test line");            
          string pattern = @"^\w*e";
          Console.WriteLine(input.ToString());
          MatchCollection matchCol = Regex.Matches(input.ToString(), pattern,RegexOptions.Multiline);//获取所有匹配结果,这个匹配结果
            foreach (Match item in matchCol)
            {
                Console.WriteLine("结果:{0}",item.Value);
            }
            Console.WriteLine("--------------");
         MatchCollection matchCol1 = Regex.Matches(input.ToString(), pattern);
            foreach (Match item in matchCol1)
            {
                Console.WriteLine("结果:{0}",item.Value);
            }
         
}
/*
void Main()
{
    StringBuilder input = new StringBuilder();
            input.AppendLine("These is the first test line");
            input.AppendLine("These is the second test line");            
          string pattern = @"^\w*e";
          Console.WriteLine(input.ToString());
          MatchCollection matchCol = Regex.Matches(input.ToString(), pattern,RegexOptions.Multiline);//获取所有匹配结果,这个匹配结果
            foreach (Match item in matchCol)
            {
                Console.WriteLine("结果:{0}",item.Value);
            }
            Console.WriteLine("--------------");
         MatchCollection matchCol1 = Regex.Matches(input.ToString(), pattern);
            foreach (Match item in matchCol1)
            {
                Console.WriteLine("结果:{0}",item.Value);
            }
         
}

*/

作者: q107770540   发布时间: 2011-12-06

C# code
   //在multilined的情况下输出是:
        结果: These
          结果: These         
          这说明这时"^"不是匹配整个字符串的开头,而是匹配一行字符串的开头。
         //第二次匹配输出的是:
          结果:These

作者: q107770540   发布时间: 2011-12-06