Multiple regex matching within a lookaround

Viewed 69

i am trying to do a multiple match within a lookbehind and a look forward

Let's say i have the following string:

$ Hi, this is an example @

Regex: (?<=$).+a.+(?=@)

I am expecting it to return both 'a' within those boundaries, is there a way to do it with only one regex?

Engine: Python

1 Answers

If you can use quantifiers in the lookbehind, use

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

See proof.

Explanation

--------------------------------------------------------------------------------
  (?<=                     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 pattern:

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

See another proof

Explanation

--------------------------------------------------------------------------------
  (?:                      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
Related