views:

514

answers:

2

I needed some code to match all IE6 versions that are not SP1 (part of Windows XP SP2, confusing eh). This is to turn gzip off for versions of IE that do not handle it properly.

The best I have come up with is:

MSIE [1-6]\.(?!.*?SV1)

Does anyone have a better pattern? What the regex above does is basically do a lookahead to make sure that the SV1 (which indicates SP1) does not exist.

For testing purposes, this should not match:

Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4325)

But this should:

Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 6.0)
A: 

Your regex does a good job of matching the user-agent you're looking for, but you should be aware that some browsers have "MSIE 6.0" in their user-agent string for compatibility. Here is a list of user-agent strings.

Jeremy Stein
A: 

Why do it with a single regex, when two would be simpler. Pseudocode:

if String.matches('\bMSIE [1-6]\b') AND NOT String.matches('\bSV1\b')
Peter Boughton