recalcitrant
recalcitrant

Reputation: 899

Using Regex to parse browser-version from User-Agent-Header

Parsing the user-agent header I need to find out if I am dealing with IE-8 or earlier:

<= IE8

So the regex should return true in the following cases:

(compatible; MSIE 8.0;...)
(compatible; MSIE 7.0;...)
etc. 

The following should yield false:

(compatible; MSIE 9.0;...)

This following regex does not work:

"MSIE [6-8]\."

Upvotes: 1

Views: 3170

Answers (2)

Robjong
Robjong

Reputation: 375

Your pattern should work if you escape the backslash, or did you mean JavaScript?. (in which case it should work)

"(?i)MSIE\\s+[5-8]\\.\\d+"

Explanation:

  • (?i) makes the match case insensitive
  • MSIE matches the string MSIE
  • \\s+ one or more spaces
  • [5-8] matches digits 5 to 8
  • \\. match a dot
  • \\d+ one or more digits

Upvotes: 4

Alex Nikolaenkov
Alex Nikolaenkov

Reputation: 2545

You can use character groups to match versions, f.i. [678]\.0.

Upvotes: 0

Related Questions