Reputation: 977
Looking for a browser agent regex to match the OS version, I have the following as test cases:
Apple~iPad~iOS~7~Mobile Safari~7.0"); << should match
Apple~iPad~iOS~7.12~Mobile Safari~7.0"); << should match
Apple~iPad~iOS~7.12.45~Mobile Safari~7.0"); << should match
Apple~iPad~iOS~7.99.451.987~Mobile Safari~7.0") << should match
Apple~iPad~iOS~blah~Mobile Safari~7.0"); << should NOT match
Apple~iPad~iOS~~Mobile Safari~7.0"); << should NOT match
Apple~iPad~iOS~7.XXXX~Mobile Safari~7.0"); << should NOT match
This gets me in the vicinity, but the last ones comes through as match on iOS~7 when it shouldn't:
iOS~(\d+)(\.\d+)?(\.\d+)?(\.\d+)?
What is the correct regex for matching desired results here?
Thanks!
Upvotes: 0
Views: 66
Reputation: 20014
How about negative look ahead of (?!\.X)
like : iOS~(\d+)(?!\.X)(\.\d+)?(\.\d+)?(\.\d+)?
Or you could use (?!\.\D)
to match anything that is not a number
Upvotes: 1
Reputation: 174706
You could use a negative lookahead,
iOS~(\d+)(?:(\.\d+)+)?(?!\.)
If you want to capture the numbers in groups, then use this
iOS~(\d+)(\.\d+)?(\.\d+)?(\.\d+)?(?!\.)
OR
You could use a positive lookahead also,
iOS~(\d+)(\.\d+)?(\.\d+)?(\.\d+)?(?=~)
Upvotes: 1
Reputation: 4069
Throw a (?!\.\D)
in there:
iOS~(\d+)(?!\.\D)(\.\d+)?(\.\d+)?(\.\d+)?
Upvotes: 1