首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lookaround中的多个正则表达式匹配

lookaround中的多个正则表达式匹配
EN

Stack Overflow用户
提问于 2020-09-25 05:24:48
回答 1查看 50关注 0票数 1

我正在尝试在后视和前瞻中进行多重匹配。

假设我有以下字符串:

代码语言:javascript
复制
$ Hi, this is an example @

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

我希望它在这些范围内返回两个'a‘,有没有办法只用一个正则表达式呢?

引擎: Python

EN

回答 1

Stack Overflow用户

发布于 2020-09-25 05:27:26

如果可以在lookbehind中使用限定符,请使用

代码语言:javascript
复制
(?<=\$[^$@]*?)a(?=[^@]*@)

参见proof

说明

代码语言:javascript
复制
--------------------------------------------------------------------------------
  (?<=                     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模式:

代码语言:javascript
复制
(?:\G(?<!^)|\$)[^$]*?\Ka(?=[^@]*@)

请参阅another proof

说明

代码语言:javascript
复制
--------------------------------------------------------------------------------
  (?:                      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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64054625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档