首页 > 解决方案 > 正则表达式前瞻量词

问题描述

我想匹配字符串b前面的最后一个。aaababaaabaab

我的正则表达式:(?<=a{2})b

但它与前面的第三个 匹配。baaa

我是初学者。

标签: regex

解决方案


利用

(?<=(?<!a)a{2})b

证明

解释

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
      a                        'a'
--------------------------------------------------------------------------------
    )                        end of look-behind
--------------------------------------------------------------------------------
    a{2}                     'a' (2 times)
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  b                        'b'

推荐阅读