日期:2014-05-18  浏览次数:20709 次

请问,这个正则表达式
下面这个例子
 class Program
  {
  static void Main(string[] args)
  {  
  string pattern = @"\b\w+es\b";
  Regex rgx = new Regex(pattern);
  string sentence = "Who writes these notes?";
  foreach (Match match in rgx.Matches(sentence))
  {
  Console.WriteLine("{0}--{1}", match.Value, match.Index);
  }
  Console.ReadKey();
  }
  }

搜索以es结尾的,最后的输出结果是:
writes--4
notes--17

我的问题是:
notes后面不是有一个问号吗?既然有?符号,怎么能说是以es结尾的呢?
谢谢!!


------解决方案--------------------
\b指单词的边界,不包括标点符号
------解决方案--------------------
如果包括标点符号,就会丢掉带有逗号句号的单词,因为\b就表示你要找单词,应该主动无视标点符号,是吧
------解决方案--------------------
探讨
那如此说来,正则表达式引擎应该对“单词”有所定义吧?什么叫单词?正则表达式引擎怎么知道“?”不是我的单词的组成部分呢?