I am trying to validate text that follows the pattern below:
- Must have the text “point(” at the beginning
- Must follow it by a Latitude numerical value with up to 5 decimal places (example: 42.12345)
- Must follow it by a comma “,”
- Must follow it by a Longitude numerical value with up to 5 decimal places (example: -81.12345)
- Must follow it by a closing parentheses “)”
Matching example:
- point(42.12345,-81.12345)
Any help is greatly appreciated.
Thanks.
You can easily build your regex with a little bit of break-up here.
point(at the beginning, use –^point\([-]?\d+(?:\.\d+)?)at the end, use\)$.For
[-]?\d+(?:\.\d+)?, here’s an explanation: –[-]?– matches an optionalnegative (-)sign at the starting (? quantifierat the end means 0 or 1)\d+– matches one or more digits(?:\.\d+)?– matches an optionaldecimal, followed byone or more. dot(.) is a special meta-character in Regex, so you need to escape it, if you want to match it.digits
Also, to limit your number of digits to
5, you can use –\d{1,5}instead of\d+, which matches minimum 1 and maximum 5 digits.^(caret)and$(dollar)anchors matches the beginning and end of the string.So, here’s your regex: –