站长资源网络编程

正则表达式问号的四种用法详解

整理:jimmy2026/7/30浏览2
简介原文符号因为在正则表达式中有特殊的含义,所以如果想匹配本身,则需要转义,\有无量词问号可以表示重复前面内容的0次或一次,也就是要么不出现,要么出现一次。非贪婪匹配贪婪匹配在满足匹配时,匹配尽可能长的字符串,默认情况下,采用贪婪匹配string pattern1 = @"a.*c"; /

原文符号

因为"htmlcode">

string pattern1 = @"a.*c";  // greedy match 
Regex regex = new Regex(pattern1);
regex.Match("abcabc"); // return "abcabc"

非贪婪匹配

在满足匹配时,匹配尽可能短的字符串,使用"htmlcode">

string pattern1 = @"a.*";  // non-greedy match 
Regex regex = new Regex(pattern1);
regex.Match("abcabc"); // return "abc"

几个常用的非贪婪匹配Pattern

*"htmlcode">

while(<>){
  if(/("Fred wants a $1\n" ;
  }
}