我们可以使用基于后备条件的条件吗?

时间:2018-12-20 22:29:52

标签: regex pcre

我有一个正则表达式,与此任务相似。这个人想要找到价格(demo),其中美元符号必须在数字之前或之后,但绝不能同时出现在两者之间。

  \b       # "word" boundary
  (?<=\$)  # Assert that current position is preceded by $
  \d+\b    # digits, boundary
  (?!\$)   # Assert that current position is not followed by $
|          # alternation
  \b       # boundary
  (?<!\$)  # Assert that current position not preceded by $
  \d+\b    # digits, boundary
  (?=\$)   # Assert that current position is followed by $

PCRE中是否有一种方法可以使用条件对条件进行相似的处理。 (demo

(                 # capture 1, because we can't quantify lookarounds
  (?<=\$)         # Asserts that $ precedes current position
)?                # close capture 1, make it "optional" (0 or 1 times)

\b                # boundary
\d+               # digits
\b                # boundary
(?(1)             # conditional that capture #1 was a success
  (?!\$)          # if successful, assert that it is not followed by a #
|
  (?=\$)          # unsuccessful, assert that this position is followed by a $
)
  

注意:单词边界很重要,因此它们都可以捕获整个数字,否则正则表达式将后退一步,将多位数字中的数字剪掉。

     

以上两个表达式都分别匹配$ 15和16 $,但不匹配$ 17 $。没有边界,它们将匹配$ 15、16和$ 1 $中的$ 1。

1 个答案:

答案 0 :(得分:0)

有效的解决方案(demo)。该问题的解决方案是99个步骤,在这个简单的示例中这是不必要的牺牲。在某些情况下,牺牲(如果有的话)过于谨慎。

这最终变得非常简单。将捕获组1设为“可能”。这意味着无论捕获到什么,如果正则表达式试图回溯,它都不会投降。在这里,这意味着一旦它决定该数字应以$开头,则以后如果该数字后面也有一个$,它就不会让正则表达式回溯。它将放弃并继续前进,找到下一个数字后的边界,并查看其周围环境。

(                 # capture 1, because we can't quantify lookarounds
  (?<=\$)         # Asserts that $ precedes current position
)?+               # close capture 1, make it "optional" (0 or 1 times)
                  # the possessive quantifier is the only change to the regex
                    # + is only 'possessive' when it's the second character of a quantifier

\b                # boundary
\d+               # digits
\b                # boundary
(?(1)             # conditional that capture #1 was a success
  (?!\$)          # if successful, assert that it is not followed by a #
|
  (?=\$)          # unsuccessful, assert that this position is followed by a $
)