环顾四周的多个正则表达式匹配

问题描述

我正在尝试在往后看和向前看中进行多次匹配

假设我有以下字符串:

$ Hi,this is an example @

正则表达式:(?<=$).+a.+(?=@)

我希望它在这些边界内返回两个“ a”,有没有办法只用一个正则表达式呢?

引擎:Python

解决方法

如果可以在后面使用量词,请使用

(?<=\$[^$@]*?)a(?=[^@]*@)

请参见proof

说明

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    \$                       '$'
--------------------------------------------------------------------------------
    [^$@]*?                  any character except: '$' and '@' (0 or more
                             times (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  a                        'a'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [^@]*                    any character except: '@' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    @                        '@'
--------------------------------------------------------------------------------
  )                        end of look-ahead

PCRE模式:

(?:\G(?<!^)|\$)[^$]*?\Ka(?=[^@]*@)

请参见another proof

说明

--------------------------------------------------------------------------------
  (?:                      group,but do not capture:
--------------------------------------------------------------------------------
    \G                       where the last m//g left off
--------------------------------------------------------------------------------
    (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
      ^                        the beginning of the string
--------------------------------------------------------------------------------
    )                        end of look-behind
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    \$                       '$'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  [^$@]*?                  any character except: '$' and '@' (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  \K                       match reset operator
--------------------------------------------------------------------------------
  a                        'a'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [^@]*                    any character except: '@' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    @                        '@'
--------------------------------------------------------------------------------
  )                        end of look-ahead