这两个术语是什么?
当前回答
来自正则表达式
regular中的标准量词 表达式是贪婪的,这意味着它们 尽可能多地匹配,只给予 回视需要进行匹配 正则表达式的剩余部分。 通过使用惰性量词,的 表达式尝试最小匹配 第一。
其他回答
贪婪匹配。正则表达式的默认行为是贪婪的。这意味着它会尝试提取尽可能多的数据,直到它符合某个模式,即使在语法上只需要较小的部分就足够了。
例子:
import re
text = "<body>Regex Greedy Matching Example </body>"
re.findall('<.*>', text)
#> ['<body>Regex Greedy Matching Example </body>']
它提取了整个字符串,而不是直到' > '第一次出现才匹配。这是regex默认的贪婪或“全部拿走”行为。
另一方面,懒惰匹配“需要的越少越好”。这可以通过添加一个?在图案的最后。
例子:
re.findall('<.*?>', text)
#> ['<body>', '</body>']
如果只希望检索第一个匹配项,则使用search方法。
re.search('<.*?>', text).group()
#> '<body>'
来源:Python Regex Examples
Greedy quantifier | Lazy quantifier | Description |
---|---|---|
* |
*? |
Star Quantifier: 0 or more |
+ |
+? |
Plus Quantifier: 1 or more |
? |
?? |
Optional Quantifier: 0 or 1 |
{n} |
{n}? |
Quantifier: exactly n |
{n,} |
{n,}? |
Quantifier: n or more |
{n,m} |
{n,m}? |
Quantifier: between n and m |
加一个?给量词,使其不贪婪,即懒惰。
例子: 测试字符串:stackoverflow 贪心reg表达式:s.*o输出:stackoverflow Lazy reg表达式:s.*?O输出:stackoverflow
为了进一步说明懒惰,这里有一个例子,乍一看可能不太直观,但从Suganthan Madhavan Pillai的回答中解释了“逐渐扩大比赛”的想法。
input -> some.email@domain.com@
regex -> ^.*?@$
这个输入的Regex将有一个匹配。乍一看,有人可能会说LAZY match(".*?@")将在第一个@停止,之后它将检查输入字符串结束("$")。按照这个逻辑,有人会得出没有匹配的结论,因为输入字符串在第一个@之后没有结束。
但正如你所看到的,情况并非如此,即使我们使用非贪婪(懒惰模式)搜索,regex也会继续前进,直到它命中秒@并有一个MINIMAL匹配。
据我所知,大多数正则表达式引擎默认是贪婪的。在量词末尾添加问号将启用惰性匹配。
正如@Andre S在评论中提到的。
贪婪:继续搜索,直到条件不满足。 Lazy:当条件满足时停止搜索。
参考下面的例子,了解什么是贪婪的,什么是懒惰的。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]){
String money = "100000000999";
String greedyRegex = "100(0*)";
Pattern pattern = Pattern.compile(greedyRegex);
Matcher matcher = pattern.matcher(money);
while(matcher.find()){
System.out.println("I'm greedy and I want " + matcher.group() + " dollars. This is the most I can get.");
}
String lazyRegex = "100(0*?)";
pattern = Pattern.compile(lazyRegex);
matcher = pattern.matcher(money);
while(matcher.find()){
System.out.println("I'm too lazy to get so much money, only " + matcher.group() + " dollars is enough for me");
}
}
}
The result is:
I'm greedy and I want 100000000 dollars. This is the most I can get.
I'm too lazy to get so much money, only 100 dollars is enough for me
试着理解以下行为:
var input = "0014.2";
Regex r1 = new Regex("\\d+.{0,1}\\d+");
Regex r2 = new Regex("\\d*.{0,1}\\d*");
Console.WriteLine(r1.Match(input).Value); // "0014.2"
Console.WriteLine(r2.Match(input).Value); // "0014.2"
input = " 0014.2";
Console.WriteLine(r1.Match(input).Value); // "0014.2"
Console.WriteLine(r2.Match(input).Value); // " 0014"
input = " 0014.2";
Console.WriteLine(r1.Match(input).Value); // "0014.2"
Console.WriteLine(r2.Match(input).Value); // ""
推荐文章
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 正则表达式中的单词边界是什么?
- 如何将一个标题转换为jQuery的URL段塞?
- Javascript和regex:分割字符串并保留分隔符
- (grep)正则表达式匹配非ascii字符?
- 如何在保持原始字符串的同时对字符串执行Perl替换?
- 创建正则表达式匹配数组
- *的区别是什么?和。*正则表达式?
- 如何将“camelCase”转换为“Camel Case”?
- 在Java中使用正则表达式提取值
- Java中的正则表达式命名组
- 使用正则表达式搜索和替换Visual Studio代码
- 使用split("|")按管道符号拆分Java字符串
- 替换字符串中第一次出现的模式
- “\d”在正则表达式中是数字吗?