贪婪的量词 [英] Greedy Quantifiers

查看:218
本文介绍了贪婪的量词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读K.Sierra并发现以下观点:

I was reading K.Sierra and found the following sentance:

贪婪的量词实际上确实读取了整个源数据,然后工作了 向后(从右边)直到找到最右边的匹配.到那时 包括从源数据的早期到现在的所有内容,并包括 是最右边的比赛的一部分.

The greedy quantifier does in fact read the entire source data, and then it works backward (from the right) until it finds the rightmost match. At that point, it includes everything from earlier in the source data up to and including the data that is part of the rightmost match.

现在,假设我们有一个来源如下:

Now, Suppose we have a source as follows:

"proj3.txt,proj1sched.pdf,proj1,proj2,proj1.java"

和模式:proj1([^,])*

为什么它与整个文本不匹配?贪婪的话,它应该与最右边的"proj1.java"匹配,返回的匹配应该是最右边的匹配之前的整个源代码?相反,它返回:

why doesn't it match the whole text? Being greedy it should have match the rightmost "proj1.java" and the returned match should have been the entire source before the right most match? Instead it returns:

proj1sched.pdf
proj1
proj1.java

推荐答案

为什么它与整个文本不匹配?

why doesn't it match the whole text?

因为您说过它必须以proj1

要贪婪,它应该与最右边的"proj1.java"匹配

Being greedy it should have match the rightmost "proj1.java"

正确.

返回的匹配项应该是最右边的匹配项之前的整个来源?

and the returned match should have been the entire source before the right most match?

不知道您为什么会这样认为或为什么会有用.如果需要的话,您可以执行.*proj1.*.

no idea why you would think that, or why that would be useful. You can just do .*proj1.* if that is what you want.

这篇关于贪婪的量词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆