regular express : how to exclude multiple character groups?

前端 未结 3 2080
既然无缘
既然无缘 2021-02-07 18:47

I have a set of urls :

/products

/categories

/customers

Now say a customers is named john, and I want to help john to reac

相关标签:
3条回答
  • 2021-02-07 18:55

    What you need here is a negative lookahead assertion. What you want to say is "I want to match any string of characters, except for these particular strings." An assertion in a regex can match against a string, but it doesn't consume any characters, allowing those character to be matched by the rest of your regex. You can specify a negative assertion by wrapping a pattern in (?! and ).

    '/^\/(?!products|categories|admin)(.+)$/'
    

    Note that you might want the following instead, if you don't allow customers names to include slashes:

    '/^\/(?!products|categories|admin)([^/]+)$/'
    
    0 讨论(0)
  • 2021-02-07 19:02

    This is entirely the wrong way to go about solving the problem, but it is possible to express fixed negative lookaheads without using negative lookaheads. Extra spacing for clarity:

    ^ (
    ( $ | [^/] |
      / ( $ | [^pc] |
        p ( $ | [^r] |
          r ( $ | [^o] |
            o ( $ | [^d] |
              d ( $ | [^u] |
                u ( $ | [^c] |
                  c ( $ | [^t] |
                    t ( $ | [^s] ))))))) |
        c ( $ | [^au] |
          a ( $ | [^t] |
            t ( $ | [^e] |
              e ( $ | [^g] |
                g ( $ | [^o] |
                  o ( $ | [^r] |
                    r ( $ | [^i] |
                      i ( $ | [^e] |
                        e ( $ | [^s] )))))))) |
          u ( $ | [^s] |
            s ( $ | [^t] |
              t ( $ | [^o] |
                o ( $ | [^m] |
                  m ( $ | [^e] |
                    e ( $ | [^r] |
                      r ( $ | [^s] ))))))))))
    ) .* ) $
    
    0 讨论(0)
  • 2021-02-07 19:12

    You're trying to use a negated character class the wrong way. A negated character class says 'do not match the contained characters'. What you are wanting to say is 'do not match if this stuff I specified here exists'. To do that you have to a bit more creative. Probably need some negative lookbehind. I'm not 100% sure about php's regex engine but something similar to this should work.

    /^\/(?<!(?:products|categories|admin))(.+)$/
    

    so, negative lookbehind (?<! ... ) saying don't match the .+ if products or categories or admin precede it. Then that is in a non-capturing group (?: ... ).

    Check out Regular Expression Advanced Syntax Reference for extra help.

    0 讨论(0)
提交回复
热议问题