我正在尝试在后视和前瞻中进行多重匹配。
假设我有以下字符串:
$ Hi, this is an example @正则表达式:(?<=$).+a.+(?=@)
我希望它在这些范围内返回两个'a‘,有没有办法只用一个正则表达式呢?
引擎: Python
发布于 2020-09-25 05:27:26
如果可以在lookbehind中使用限定符,请使用
(?<=\$[^$@]*?)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-aheadPCRE模式:
(?:\G(?<!^)|\$)[^$]*?\Ka(?=[^@]*@)说明
--------------------------------------------------------------------------------
(?: 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-aheadhttps://stackoverflow.com/questions/64054625
复制相似问题