問號可以表示重復前面內容的0次或一次,也就是要么不出現,要么出現一次
示例1:
string pattern1 = @"a.*?c"; // non-greedy match Regex regex = new Regex(pattern1); regex.Match("abcabc"); // return "abc"
結果:abc
示例2:
import re s='hello 1234567 world' res = re.match('he.*?(\d).*rld$',s) print(res.group(1))
結果:123456
常用非貪婪表達式
*? 重復任意次,但盡可能少重復 +? 重復1次或更多次,但盡可能少重復 ?? 重復0次或1次,但盡可能少重復 {n,m}? 重復n到m次,但盡可能少重復 {n,}? 重復n次以上,但盡可能少重復
https://www.cnblogs.com/graphics/archive/2010/06/02/1749707.html