开发者

Ruby simple regex syntax error

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 wron开发者_Python百科g.


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/


[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-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/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜