views:

185

answers:

4

I have a string:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)

I want to know what version of Firefox is in the string (3.5.2).

My current regex is:

Firefox\/[0-9]\.[0-9]\.[0-9]

and it returns Firefox/3.5.2

I only want it to return 3.5.2 from the Firefox version, not the other versions in the string. I already know the browser is Firefox.

A: 
Firefox\/([0-9]\.[0-9]\.[0-9])

and extract match #1, however this is done in your (unspecified, though one suspects JavaScript) regex engine. Or, if this is very annoying to do, and your regex supports lookbehind:

(?<=Firefox\/)[0-9]\.[0-9]\.[0-9]
chaos
+1  A: 

Firefox\/([0-9]\.[0-9]\.[0-9])

Depending on your language (i'm assuming js) it'll be the second element in the array

i.e.


var regex = /Firefox\/([0-9]\.[0-9]\.[0-9])/
var matches = useragent.match(regex);
alert(matches[1]); // 3.5.2
rezzif
d'oh missed it by that much :P
rezzif
+1  A: 
Firefox\/([0-9]\.[0-9]\.[0-9])

Create a capture group around the numbers like I have done above with the (). Then the regex you want will be in the 2nd index in the array that is returned. e.g for zero based languages matchedArray[1] and or 1 based languages its matchedArray[2]

AutomatedTester
+1  A: 
(?<=Firefox/)\d+\.\d+\.\d+

will return 3.5.2 as the entire match (using lookbehind - which is not available in all regex flavors, though, especially JavaScript).

So, if it has to be JavaScript, search for Firefox/(\d+\.\d+\.\d+) and use match no. 1.

Since in theory there could be more than one digit (say, version 3.10.0), I've also changed that part of the regex, allowing for one or more digits for each number. Also, there is no need to escape the slash.

Tim Pietzcker