Kevin
Kevin

Reputation: 2888

Regex to detect IE9 and below?

I am trying to write a regex pattern in Java to detect if a user agent is less than or equal to Internet Explorer 9. There are quite a few examples here:

http://www.useragentstring.com/pages/Internet%20Explorer/

The main gist is there will be a string in the user agent string called: "MSIE XXXX). My current regex pattern is this:

MSIE ([1-9]|9)

Which seems to work except there are problems when I do MSIE 10.0 or MSIE 11.0 it matches. Any ideas on how to only match 1-9 and no 10.0 or 11?

Upvotes: 2

Views: 1210

Answers (2)

Guillaume
Guillaume

Reputation: 13138

In old versions, there is always a dot after the major version number, you can use it in your regex :

MSIE [1-9]\.

Another way to write it is to expect one number between 1 and 9 followed by a non number char :

MSIE [1-9][^0-9]

Upvotes: 2

Sean256
Sean256

Reputation: 3099

only match 5-9 and not 1-9. There are virtually 0 people using IE 4 and below.

Upvotes: 1

Related Questions