I am trying to learn regex for my project I am working on to use it for input validation because I have been told that using regex is one of the best ways to do input validation.
So, I am trying to make sure a string contains an unknown number of coordinates which are separated by a space. An example of what the input will look like is
2,2 23.45,6 45,21.65 2,2
-2.3,2 34,-2 -2.3,2\n
You can try this regex:
Note: The regex
\d+(?:\.\d+)? matches any number, possibly having a decimal component. The
?: inside the parenthesis marks the quantity as a non capture group. This tells the regex engine to not capture what is inside, as we don't want to actually capture anything here. This possibly will result in a more efficient regex.