懒惰,贪婪和占有量词之间有什么区别? [英] What are the differences between lazy, greedy and possessive quantifiers?

查看:53
本文介绍了懒惰,贪婪和占有量词之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下量词有何不同 - 在场景、速度等方面.

How do the following quantifiers differ - with respect of scenarios, speed, etc.

  • ????+ 都匹配 0 或 1 次.
  • *, *?和*+` 都匹配0 次或多次.
  • ++?++ 都匹配1 次或多次.
  • ?, ?? and ?+ all match 0 or 1 times.
  • *, *? and*+` all match 0 or more times.
  • +, +? and ++ all match 1 or more times.
  • ?*+贪婪.
  • ??*?+?不情愿/懒惰.
  • ?+*+++所有物.
  • ?, * and + are greedy.
  • ??, *? and +? are reluctant/lazy.
  • ?+, *+ and ++ are possessive.

谁能帮助我理解这些术语的含义?为什么同一份工作的每个量词有三种变体?

Can anyone help me to understand what these terms mean? Why are there three variations of each quantifier for the same job?

推荐答案

取字符串

aaaab

并查看以下正则表达式如何匹配它:

and see how the following regexes match it:

Regex          Submatches
               group 1  group 2  group3
(a?)(a*)(ab)   a        aa       ab
(a??)(a*)(ab)           aaa      ab
(a?+)(a*)(ab)  a        aa       ab
(a*)(a?)(ab)   aaa               ab
(a*?)(a?)(ab)  aa       a        ab
(a*+)(a?)(ab)  aaaa              <Match fails!>
(a+)(a*)(ab)   aaa               ab 
(a+?)(a*)(ab)  a        aa       ab
(a++)(a*)(ab)  aaaa              <Match fails!>

说明:

  • a? 尝试匹配一个 a,但如果整个匹配成功有必要,它准备不匹配任何内容.
  • a?? 尝试不匹配任何内容,但它准备匹配一个 a,如果这是整个匹配成功所必需的.
  • a?+ 尝试匹配一个 a.如果它可以做到这一点,它就不会退缩以匹配任何东西,如果这对于整个匹配成功是必要的.如果它不能匹配 a,那么它很乐意什么都不匹配.
  • a* 尝试匹配尽可能多的 as,但它准备匹配更少的 as,即使没有整个比赛取得成功所必需的.
  • a*? 尝试不匹配任何内容,但它准备匹配尽可能多的 a 以确保整个匹配成功,但不会更多.
  • a*+ 尝试匹配尽可能多的 a .如果它可以做到这一点,它就不会回退以匹配更少的a,如果这是整个匹配成功所必需的.如果它连一个 a 都不能匹配,那么它很乐意什么都不匹配.
  • a+ 尝试匹配尽可能多的 as,但它准备匹配更少的 as(但至少有一个)如果这对于整场比赛的成功来说是必要的.
  • a+? 尝试只匹配一个 a,但它准备匹配尽可能多的 a整场比赛成功,但不会更多.
  • a++ 尝试匹配尽可能多的 a .如果它可以做到这一点,它就不会回退以匹配更少的a,如果这是整个匹配成功所必需的.如果它甚至不能匹配单个 a,那么正则表达式就会失败.
  • a? tries to match one a, but it's prepared to match nothing if that's necessary for the whole match to succeed.
  • a?? tries to match nothing, but it's prepared to match one a if that's necessary for the whole match to succeed.
  • a?+ tries to match one a. If it can do that, it will not back down to match nothing if that were necessary for the overall match to succeed. If it can't match an a, then it will gladly match nothing, though.
  • a* tries to match as many as as it can, but it's prepared to match fewer as, even nothing if that's necessary for the whole match to succeed.
  • a*? tries to match nothing, but it's prepared to match just as many as as is absolutely necessary in order for the whole match to succeed, but not more.
  • a*+ tries to match as many as as it can. If it can do that, it will not back down to match fewer as if that were necessary for the overall match to succeed. If it can't match even a single a, then it will gladly match nothing, though.
  • a+ tries to match as many as as it can, but it's prepared to match fewer as (but at least one) if that's necessary for the whole match to succeed.
  • a+? tries to match only one a, but it's prepared to match just as many as as is absolutely necessary in order for the whole match to succeed, but not more.
  • a++ tries to match as many as as it can. If it can do that, it will not back down to match fewer as if that were necessary for the overall match to succeed. If it can't match even a single a, then the regex fails.

这篇关于懒惰,贪婪和占有量词之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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