zoukankan      html  css  js  c++  java
  • Java regex quantifiers

    1. Enter your regex: .*foo  // greedy quantifier
      Enter input string to search: xfooxxxxxxfoo
      I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.
      
      Enter your regex: .*?foo  // reluctant quantifier
      Enter input string to search: xfooxxxxxxfoo
      I found the text "xfoo" starting at index 0 and ending at index 4.
      I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.
      
      Enter your regex: .*+foo // possessive quantifier
      Enter input string to search: xfooxxxxxxfoo
      No match found.
    2. Explain: (see http://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-quantifiers)

      greedy quantifier first matches as much as possible. So the .* matches the entire string. Then the matcher tries to match the f following, but there are no characters left. So it "backtracks", making the greedy quantifier match one less thing (leaving the "o" at the end of the string unmatched). That still doesn't match the f in the regex, so it "backtracks" one more step, making the greedy quantifier match one less thing again (leaving the "oo" at the end of the string unmatched). That still doesn't match thef in the regex, so it backtracks one more step (leaving the "foo" at the end of the string unmatched). Now, the matcher finally matches the f in the regex, and the o and the next o are matched too. Success!

      reluctant or "non-greedy" quantifier first matches as little as possible. So the .* matches nothing at first, leaving the entire string unmatched. Then the matcher tries to match the f following, but the unmatched portion of the string starts with "x" so that doesn't work. So the matcher backtracks, making the non-greedy quantifier match one more thing (now it matches the "x", leaving "fooxxxxxxfoo" unmatched). Then it tries to match the f, which succeeds, and the o and the next o in the regex match too. Success!

      In your example, it then starts the process over with the remaining unmatched portion of the string, following the same process.

      possessive quantifier is just like the greedy quantifier, but it doesn't backtrack. So it starts out with.* matching the entire string, leaving nothing unmatched. Then there is nothing left for it to match with the f in the regex. Since the possessive quantifier doesn't backtrack, the match fails there.

  • 相关阅读:
    资金流学习-成本分析
    解决root用户不能打开Chromium网页浏览器
    Kali Linux 2020.1乱码问题
    Kali Linux安装谷歌浏览器
    解决Kali Linux 2020.1乱码问题
    Kali Linux 2020.1a版本msfconsole启动失败问题
    Kali Linux发布2020.1a版本
    Kali Linux 2020.1快速修改root用户密码
    Kali Linux 2020.1安装桌面
    Kali Linux 2020.1修改系统语言
  • 原文地址:https://www.cnblogs.com/wade-case/p/3380253.html
Copyright © 2011-2022 走看看