Search code examples
regexprefix

How to ignore a Regex match, if there is a specific prefix


I am using this expression to find paths.

([^\s\&\=\;\,\<\<\>\"\'\(\)]+\/[\w\/])([^\"\'\n\;\}\)\s]*)

Current matches are:

Location: /user/login.php xx... /user/login xx... text/html abcd type text/html hey abc/def

I want to ignore match which has "type " prefix

So It should be like this.

Location: /user/login.php xx... /user/login xx... text/html abcd type text/html hey abc/def


Solution

  • For those separate matches, you can assert a whitespace boundary to the left or not being precede by type

    Note that most of the characters that you have in the character class do not need escaping by itself.

    (?<!\S|\btype )[^\/\s&=;,<>"'()]*(?:\/[^"'\n;})\s]+)+
    

    In parts, the pattern matches:

    • (?<!\S|\btype ) Negative lookbehind, a whitespace boundary to the left or not type
    • [^\/\s&=;,<>"'()]* Optionally match 1 of the listed characters
    • (?: Non capture group
      • \/[^"'\n;})\s]+ Match 1+ times any of the listed characters
    • )+ Close non capture group and repeat 1+ times to match at least a single occurrence of /

    Regex demo