Question

ex. UserAgent (accepted) : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36

I woud like create regex who accepted only UserAgent who start by Mozilla but not contain the bot word

ex. not accepted :

Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

Thanks

Was it helpful?

Solution

Usually, look ahead are a good way to create condition one the string inside one regex.

^(?!.*bot)Mozilla.*$
  • (?!...) is a negative lookahead. It will fail the whole regex if what's inside matches, so here if there is bot somewhere in your string (even inside a word)
  • ^$ are anchors matching the beginning and the end of your string

Demo: http://regex101.com/r/iN4zA7

OTHER TIPS

Use negative look aheads for this challenge: (?!bot)

^Mozilla((?!bot).)*$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top