Regex 如果后跟斜杠,则正则表达式不匹配

Regex 如果后跟斜杠,则正则表达式不匹配,regex,Regex,所以我想匹配一个如下所示的字符串: apple/banana 因此,我的正则表达式看起来像apple\s*/\\s*banana 但我不想匹配后面是斜杠的情况,比如: apple/banana/或apple/banana/但如果后面没有像apple/banana juice/chocolate这样的斜杠,则仍将匹配 我如何做到这一点? 感谢您的帖子,您似乎不想匹配以'/'结尾的字符串 /^.+(? 正则表达式演示。 那么这个正则表达式是如何工作的呢 + 这与除换行符之外的所有内容都匹配 ( 这

所以我想匹配一个如下所示的字符串:
apple/banana

因此,我的正则表达式看起来像
apple\s*/\\s*banana

但我不想匹配后面是斜杠的情况,比如:
apple/banana/
apple/banana/
但如果后面没有像
apple/banana juice/chocolate这样的斜杠,则仍将匹配

我如何做到这一点?
感谢您的帖子,您似乎不想匹配以
'/'
结尾的字符串

/^.+(?
正则表达式演示。

那么这个正则表达式是如何工作的呢

  • +

    这与除换行符之外的所有内容都匹配

  • 这是否定的lookbehind断言,它检查字符串末尾是否没有
    '/'
    字符

  • ^…$

    确保从头到尾的匹配


  • 从您的帖子中,似乎不希望匹配以
    '/'
    结尾的字符串

    /^.+(?
    正则表达式演示。

    那么这个正则表达式是如何工作的呢

  • +

    这与除换行符之外的所有内容都匹配

  • 这是否定的lookbehind断言,它检查字符串末尾是否没有
    '/'
    字符

  • ^…$

    确保从头到尾的匹配


  • 请您尝试以下方法:

    ^[^\/\n]+(?:\/[^\/\n]+)+$
    
    结果:

    apple                           no match
    apple/banana                    match
    apple/banana/                   no match
    apple/banana /                  no match
    apple/banana/mango              match
    apple/banana/mango/             no match
    apple/banana juice/chocolate    match
    

    请尝试以下操作:

    ^[^\/\n]+(?:\/[^\/\n]+)+$
    
    结果:

    apple                           no match
    apple/banana                    match
    apple/banana/                   no match
    apple/banana /                  no match
    apple/banana/mango              match
    apple/banana/mango/             no match
    apple/banana juice/chocolate    match
    

    您可以使用负前瞻来声明前面不带可选空格字符(不包括换行符)的
    /

    \bapple/banana\b(?![^\S\r\n]*/)
    
    解释

    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      apple                    'apple'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \/                       '/'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      banana                   'banana'
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                 or more times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        \/                       '/'
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    
    • \bapple
      匹配前面带有单词边界的apple,使其不匹配,例如菠萝
    • /
      匹配
      /
      (正斜杠本身不需要转义,也不用作分隔符)
    • banana\b
      Match
      banana
      后跟一个不匹配的单词边界,例如banana
    • (?!
      消极前瞻,断言正确的东西不正确
      • [^\S\r\n]*/
        匹配0+不包括换行符的whitspace字符,然后匹配
        /
    • 关闭前瞻

    您可以使用负前瞻来声明前面不带可选空格字符(不包括换行符)的
    /

    \bapple/banana\b(?![^\S\r\n]*/)
    
    解释

    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      apple                    'apple'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \/                       '/'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      banana                   'banana'
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                 or more times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        \/                       '/'
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    
    • \bapple
      匹配前面带有单词边界的apple,使其不匹配,例如菠萝
    • /
      匹配
      /
      (正斜杠本身不需要转义,也不用作分隔符)
    • banana\b
      Match
      banana
      后跟一个不匹配的单词边界,例如banana
    • (?!
      消极前瞻,断言正确的东西不正确
      • [^\S\r\n]*/
        匹配0+不包括换行符的whitspace字符,然后匹配
        /
    • 关闭前瞻
    使用

    \bapple\s*\/\s*banana\b(?!\s*\/)
    

    解释

    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      apple                    'apple'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \/                       '/'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      banana                   'banana'
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                 or more times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        \/                       '/'
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    
    使用

    \bapple\s*\/\s*banana\b(?!\s*\/)
    

    解释

    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      apple                    'apple'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \/                       '/'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      banana                   'banana'
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                 or more times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        \/                       '/'
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    

    取决于您的正则表达式引擎。请参阅,然后按照此处的指导。关于
    果汁/苹果/香蕉
    ?尝试使用@Ilyes
    苹果/香蕉汁/巧克力/
    进行匹配。@Ava操作状态“如果不直接后跟斜杠,仍将匹配”取决于您的正则表达式引擎。请参阅,然后按照此处的指导。关于
    果汁/苹果/香蕉
    ?尝试使用@Ilyes
    苹果/香蕉汁/巧克力/
    进行匹配。@Ava操作状态“如果不直接后跟斜杠,仍将匹配”当然++,我完全忘记了斜杠周围的whitspace字符。当然++,我完全忘记了斜杠周围的whitspace字符