主要差别在使用了RegexOptions.Multiline多行模式上,看下面两个示例:string pattern = @"^abc"; string str = "zzz\nabc"; Regex regex = new Regex(pattern, RegexOptions.Multiline); bool b = regex.IsMatch(str); Console.WriteLine(b); //Truestring pattern = @"\Aabc"; string str = "zzz\nabc"; Regex regex = new Regex(pattern, RegexOptions.Multiline); bool b = regex.IsMatch(str); Console.WriteLine(b); //False再看MSDN的叙述:^ 指定匹配必须出现在字符串的开头或行的开头。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。 $ 指定匹配必须出现在以下位置:字符串结尾、字符串结尾的 \n 之前或行的结尾。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。 \A 指定匹配必须出现在字符串的开头(忽略 Multiline 选项)。 \Z 指定匹配必须出现在字符串的结尾或字符串结尾的 \n 之前(忽略 Multiline 选项)。 \z 指定匹配必须出现在字符串的结尾(忽略 Multiline 选项)。 最后看一下多行模式:(恕不翻译) Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.
string str = "zzz\nabc";
Regex regex = new Regex(pattern, RegexOptions.Multiline);
bool b = regex.IsMatch(str);
Console.WriteLine(b);
//Truestring pattern = @"\Aabc";
string str = "zzz\nabc";
Regex regex = new Regex(pattern, RegexOptions.Multiline);
bool b = regex.IsMatch(str);
Console.WriteLine(b);
//False再看MSDN的叙述:^
指定匹配必须出现在字符串的开头或行的开头。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。 $
指定匹配必须出现在以下位置:字符串结尾、字符串结尾的 \n 之前或行的结尾。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。 \A
指定匹配必须出现在字符串的开头(忽略 Multiline 选项)。 \Z
指定匹配必须出现在字符串的结尾或字符串结尾的 \n 之前(忽略 Multiline 选项)。 \z
指定匹配必须出现在字符串的结尾(忽略 Multiline 选项)。
最后看一下多行模式:(恕不翻译)
Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.