正则表达式?非贪婪匹配

问号可以表示重复前面内容的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

猜你喜欢

转载自www.cnblogs.com/zhaogaojian/p/10320299.html