代码之家  ›  专栏  ›  技术社区  ›  Michael Choi Peter Alexander

regex匹配不被空格包围

  •  1
  • Michael Choi Peter Alexander  · 技术社区  · 6 年前

    上下文:我正在构建一个有趣的解释程序,需要实现一个空白规则。

    规则是,当不被空格包围时,regex必须与加号“+”匹配。 以下是示例文本:

    a+b c +d g+ h i + j x+y
    

    两者之间的优势 A和B 应该同时匹配 X和Y . 我不熟悉正则表达式;但是,我尝试了以下regex:

    \+(?<!\s)(?!\s)
    

    对我来说意味着,

    \+         find all "+" matches
    (?<!\s)   (neg look behind) that don't have spaces behind
    (?!\s)     (neg look ahead) that don't have spaces ahead
    

    但是,当我在regex101上运行这个函数时,我只能在 A和B .

    我不确定我在这里做的不对。感谢您的建议。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Casimir et Hippolyte    6 年前