正则表达式-定位点

匹配开头的结尾的,主要差别在使用了RegexOptions.Multiline多行模式上,看下面两个示例:

string pattern = @"^abc";
string str = "zzz
abc";
Regex regex = new Regex(pattern, RegexOptions.Multiline);
bool b = regex.IsMatch(str);
Console.WriteLine(b);
//True

string pattern = @"Aabc";
string str = "zzz
abc";
Regex regex = new Regex(pattern, RegexOptions.Multiline);
bool b = regex.IsMatch(str);
Console.WriteLine(b);
//False



再看MSDN的叙述:


指定匹配必须出现在字符串的开头或行的开头。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。 


指定匹配必须出现在以下位置:字符串结尾、字符串结尾的   之前或行的结尾。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。 


指定匹配必须出现在字符串的开头(忽略 Multiline 选项)。 


指定匹配必须出现在字符串的结尾或字符串结尾的   之前(忽略 Multiline 选项)。 


指定匹配必须出现在字符串的结尾(忽略 Multiline 选项)。

连续匹配:G
 

G 定位标记指定匹配必须出现在上一个匹配结束的地方。 通过 Regex.Matches 或 Match.NextMatch 方法使用此定位点时,它确保所有匹配项是连续的。

下面的示例使用正则表达式从以逗号分隔的字符串提取啮齿类的名称。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "capybara,squirrel,chipmunk,porcupine,gopher," + 
                     "beaver,groundhog,hamster,guinea pig,gerbil," + 
                     "chinchilla,prairie dog,mouse,rat";
      string pattern = @"G(w+s?w*),?";
      Match match = Regex.Match(input, pattern);
      while (match.Success) 
      {
         Console.WriteLine(match.Groups[1].Value);
         match = match.NextMatch();
      } 
   }
}
// The example displays the following output:
//       capybara
//       squirrel
//       chipmunk
//       porcupine
//       gopher
//       beaver
//       groundhog
//       hamster
//       guinea pig
//       gerbil
//       chinchilla
//       prairie dog
//       mouse
//       rat
单词边界:
 

 定位标记指定匹配必须出现在单词字符(w 语言元素)和非单词字符(W 语言元素)之间的边界上。 单词字符包含字母数字字符和下划线;非单词字符是非字母数字或下划线的任何字符。(有关更多信息,请参见正则表达式中的字符类。)匹配也可能出现在字符串的开头或结尾的单词边界。

 定位标记经常用于确保子表达式匹配整个单词而不只是匹配单词的开头或结尾。 下例中的正则表达式 arew* 演示此用法。 它匹配以子字符串“are”开头的任意单词。 示例的输出还演示  同时匹配输入字符串的开头和结尾。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "area bare arena mare";
      string pattern = @"arew*";
      Console.WriteLine("Words that begin with 'are':");
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("'{0}' found at position {1}",
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       Words that begin with 'are':
//       'area' found at position 0
//       'arena' found at position 10
非字边界:B
 

B 定位标记指定匹配不得出现在单词边界。 它是与  定位点相反的定位点。

下面的示例使用 B 定位点找到单词中子字符串“qu”的匹配项。 正则表达式模式 Bquw+ 匹配以“qu”开头的子字符串,该字符串不会开始单词并延续到字符串的结尾。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "equity queen equip acquaint quiet";
      string pattern = @"Bquw+";
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("'{0}' found at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       'quity' found at position 1
//       'quip' found at position 14
//       'quaint' found at position 21
原文地址:https://www.cnblogs.com/qixuejia/p/4217843.html