正则表达式中的双加号是什么?

Tee*_*eej 28 php regex

我在一些PHP脚本中看到过这个:

[a-zA-Z0-9_]++
Run Code Online (Sandbox Code Playgroud)

双加是什么意思?

Kob*_*obi 34

这是一个占有量词.

它基本上意味着如果正则表达式引擎稍后失败匹配,它将不会返回并尝试撤消它在此处所做的匹配.在大多数情况下,它允许引擎更快发生故障,并且可以在您需要的地方为您提供一些控制 - 这在大多数情况下非常罕见.

  • 只是为了清除它:它可以撤消*整个*组,但组*内没有*.我不确定这是否足够清楚,但链接解释得非常好. (3认同)
  • 你能举例说明差异++和+吗? (3认同)

And*_*ico 14

给你一个非常简单的例子:

假设你有一个字符串"123".^在以下示例中,匹配的字符具有下方.

  1. 正则表达式:\d+?. 部分匹配!

    123  # The \d+? eats only 1 because he's lazy (on a diet) and leaves the 2 to the .(dot).
    ^^   # This means \d+? eats as little as possible.
    
    Run Code Online (Sandbox Code Playgroud)
  2. 正则表达:\d+. 完全匹配!

    123  # The \d+ eats 12 and leaves the 3 to the .(dot).
    ^^^  # This means \d+ is greedy but can still share some of his potential food to his neighbour friends.
    
    Run Code Online (Sandbox Code Playgroud)
  3. 正则表达式:\d++. 没有比赛!

    123  # The \d++ eats 123. He would even eat more if there were more numbers following. 
         # This means \d++ is possessive. There is nothing left over for the .(dot), so the pattern can't be matched.
    
    Run Code Online (Sandbox Code Playgroud)