0

I'm new to Ruby and i'm working on a code for a server. The code is a half year old. In the meanwhile Chrome updated versions to version 14.

So here is my code:

supported_browsers = /Chrome\/[3-9]|Firefox\/[3-9]|\sMSIE\s|Konqueror\/[4-9]|Midori|Minefield|Shiretoko|IceCat|Opera\/9.|\sAppleWebKit/

As you can see Chrome 3-9 but now when i try to change it to:

supported_browsers = /Chrome\/[3-15]|Firefox\/[3-9]|\sMSIE\s|Konqueror\/[4-9]|Midori|Minefield|Shiretoko|IceCat|Opera\/9.|\sAppleWebKit/

I get a syntax error. Help me figure out what's wrong.

3 Answers 3

3

Your error is here : [3-15] this is a character class with range of char from 3 to 1 that is not allowed.

I guees you want : [3-9]|1[0-5] that means 3 to 9 or 10 to 15

The complete regex is:

supported_browsers = /Chrome\/([3-9]|1[0-5])|Firefox\/[3-9]|\sMSIE\s|Konqueror\/[4-9]|Midori|Minefield|Shiretoko|IceCat|Opera\/9.|\sAppleWebKit/
Sign up to request clarification or add additional context in comments.

Comments

3

[3-9] is a numeric range. It means a single digit between 3 or 9. Numeric ranges doesn't work in the way you expect: [3-15] is not a valid range.

If you simply want to match a digit range you can use [0-9]{1,2}. It matches everything between 0 and 99. Or [0-9]+ to make it less restrictive.

supported_browsers = /Chrome\/[0-9]+|Firefox\/[3-9]|\sMSIE\s|Konqueror\/[4-9]|Midori|Minefield|Shiretoko|IceCat|Opera\/9.|\sAppleWebKit/

If you really want to validate the inclusion in the range 3-15, using regular expressions is not really the best choice. In fact, using regular expression your range should be [3-9]|1[0-5] and the more restrictive you want to be, the more complicated the regexp becomes.

supported_browsers = /Chrome\/(?:[3-9]|1[0-5])|Firefox\/[3-9]|\sMSIE\s|Konqueror\/[4-   9]|Midori|Minefield|Shiretoko|IceCat|Opera\/9.|\sAppleWebKit/

3 Comments

Why isn't it the best choice?
I told you in the last sentence: the more sophisticated the match becomes, the more complex is your regexp. There is a point where you need to leave regexp and use code. For example, what would happen if you don't support Chrome 13? Validating a range 3-15 except 13 using a regexp forces you to write extremely complicated regexps.
Anyway, for now you can still use regexps but remember, when exceptions will arrive, don't try to solve everything in a regexp or simplify the problem. Do you really need to check the version number? Keep in mind that Chrome has a built-in update system, it's very unlikely that a user has a Chrome version < 3.
1

[3-15] does not check the range. for range you have to use [3-9]|1[0-4] would match 1-14 e.g.

supported_browsers = /Chrome\/([3-9]|1[0-4])|Firefox\/[3-9]|\sMSIE\s|Konqueror\/[4-9]|Midori|Minefield|Shiretoko|IceCat|Opera\/9.|\sAppleWebKit/

1 Comment

[1-9][0-4] also matches 94.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.