I've been googling a lot about this. What I'm trying to achieve is the next: I have to check in a regex condition if a MIME is NOT of a specific type.
For example, I have recevied the next message:
image/png, image/jpeg, document/pdf
I would like to detect the document/pdf
part only
, which is a MIME type, a string, that does NOT start with image/
But no matter how hard I looked, tried and played around with the RegExBody software, I just utterly fail to match it..
I'm posting this in despair and hopes that maybe an expert regex could help me out..
I tried many approaches, mainly: Finding out the non-image type, regardless if there is one or not.
It just refuses to work.
I tried positive lookahead and negative lookahead. But I probably used it wrong somehow. I can't post the examples because I have tried and deleted so many. The one that seemed really close to working was \b(?:(?!image/\w+))\w+\b
but it just persists on selecting the second part of the non-matching pattern.
If I use: image/png
It gets the: png
Which means it would still return true although I meant it to ignore image/
types..
It's a negative lookahead, which means that for the expression to match, the part within (?!...) must not match. In this case the regex matches http:// only when it is not followed by the current host name (roughly, see Thilo's comment).
The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.
The (?!...) part means "only match if the text following (hence: lookahead) this doesn't (hence: negative) match this. But it doesn't actually consume the characters it matches (hence: zero-width). lookbehind / lookahead : specifies if the characters before or after the point are considered.
You should have added a /\w+
part after \w+
to match your substring:
\b(?!image/)\w+/\w+\b
See the regex demo
Pattern details:
\b
- word boundary(?!image/)
- a negative lookahead failing the match if there is image/
right after the current location\w+/\w+
- 1+ word characters followed with /
and again 1+ word characters\b
- a trailing word boundaryIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With