Reputation: 595
I am new to regexp. I need to validate a string, but when I use my current attempt it is always returning false.
Rules:
eg:
PolyGoN((
-74.0075783459999 40.710775696,
-74.007375926 40.710655064,
-74.0074640719999 40.7108592490001,
-74.0075783459999 40.710775696))
Here is the code that I used:
String inputString = "POLYGON((-74.0075783459999 40.710775696, -74.007375926 40.710655064, -74.0072836009999 40.710720973, -74.0075783459999 40.710775696))";
String regexp = "polygon[\\((][(\\-?\\d+(\\.\\d+)?)\\s*(\\-?\\d+(\\.\\d+)?)]*[\\))]";
Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputString);
boolean result = matcher.matches();
Upvotes: 0
Views: 894
Reputation: 51711
[\\((]
is incorrect way of specifying that you need (
twice. No matter how many times you repeat a character but inside a character class []
it counts only once. Since, it's the same character repeating you don't even need a character class there but just the character \\(
with a quantifier that tells how many times it should repeat {2}
. So, you need \\({2}
at the start and \\){2}
at the end.
Another problem with your use of []
is that you used them to denote a group of double pairs that repeats (using *
). You always use ()
for grouping a part of your match. []
denotes a character class only. I wonder why you got that wrong because you grouped your doubles and their pairs correctly.
Next, you've forgotten to match all the commas ,
separating the double pairs. I've included that as (,\\s*)?
in my regex. The hyphen -
(or the negative sign here) doesn't need to be escaped since it's not inside a character class []
and so the regex parser knows you're not using it to specify a character range.
The corrected regex is (indented for clarity)
polygon\({2}\s*(
(-?\d+(\.\d+)?)\s*(-?\d+(\.\d+)?)(,\s*)
)*(-?\d+(\.\d+)?)\s*(-?\d+(\.\d+)?)
\s*\){2}
Upvotes: 3
Reputation: 4322
m|Polygon\(\(((\s*-?\d+\.\d+\s*){2},)*(\s*-?\d+\.\d+\s*){2}\)\)|i
Upvotes: 0