Reputation: 67
import java.util.ArrayList;
import java.util.List;
public class ValidateDemo {
public static void main(String[] args) {
List<String> input = new ArrayList<String>();
input.add("2005-WW-1");
input.add("3012-W-223");
input.add("1952-WX-431");
input.add("19998-d-4134");
input.add("1899-c-7465");
for (String car : input) {
if (car.matches("^\b(19[3-9][0-9]|200[0-9]|201[0-2])\b-?(KK|kk|ww|WW|c|C|ce|CE|cn|CN|cw|CW|d|D|dl|DL|g|G|ke|KE|ky|KY|l|L|ld|LD|lh|LH|lk|LK|lm|LM|ls|LS|mh|MH|mn|MN|mo|MO|oy|OY|so|SO|rn|RN|tn|TN|ts|TS|w|W|wd|WD|wh|WH|wx|WX)-?\\d{1,4}\\d)$")) {
System.out.println("Car Template " + car);
}
}
}
}
I'm trying to get it to match car reg plates from years 1980-2015 (I know that's not what I have in my pattern) But i keep getting a "Unmatched closing ')' " Error.
So for example "2005-WW-1" should be the only input that will match the pattern.
Upvotes: 0
Views: 601
Reputation: 40328
I'd suggest something like Pshemo's answer, implemented as:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
public class ValidateDemo
{
public static void main(String[] args) {
List<String> input = new ArrayList<String>();
input.add("2005-WW-1");
input.add("3012-W-223");
input.add("1952-WX-431");
input.add("19998-d-4134");
input.add("1899-c-7465");
String re = "^(\\d+)-?(KK|WW|C|CE|CN|CW|D|DL|G|KE|KY|L|LD|LH|LK|LM|LS|MH|MN|MO|OY|SO|RN|TN|TS|W|WD|WH|WX)-?\\d{1,4}$";
Pattern p = Pattern.compile(re, Pattern.CASE_INSENSITIVE);
for (String car : input) {
Matcher m = p.matcher(car);
System.out.print(car + " - " + m.matches() + " - ");
System.out.print(m.group(1));
int year = Integer.parseInt(m.group(1));
if( year >= 1980 && year <= 2015 ) System.out.println(" <-- CORRECT");
else System.out.println();
}
}
}
NOTES:
Upvotes: 0
Reputation: 46239
\b
should be \\b
\\d
at the end (or change the one before it to {2,5} if you only want to match 2 to 5)So, this works:
if (car.matches("^\\b(19[3-9][0-9]|200[0-9]|201[0-2])\\b-?(KK|kk|ww|WW|c|C|ce|CE|cn|CN|cw|CW|d|D|dl|DL|g|G|ke|KE|ky|KY|l|L|ld|LD|lh|LH|lk|LK|lm|LM|ls|LS|mh|MH|mn|MN|mo|MO|oy|OY|so|SO|rn|RN|tn|TN|ts|TS|w|W|wd|WD|wh|WH|wx|WX)-?\\d{1,4}$")) {
Also note that this matches plates from 1930 onwards, not 1980 as you specify in your text.
Upvotes: 0
Reputation: 44438
Your regex contains 2 opening brackets ((
) and 3 closing brackets ()
).
The error
Unmatched closing ')'
Is fairly descriptive.
I'm assuming you have to add an opening bracket somewhere near the end here:
-?\\d{1,4}\\d)$
My regex is inadequate, but I believe this is what you want:
-?(\\d{1,4}\\d)$
Why don't you just get the first 4 characters and see if they're in a range?
var year = Integer.Parse(myString.SubString(0, 4)); // Error checking omitted
if(year > 1985 && year < 2005) { }
Upvotes: 0
Reputation: 124285
-?\\d{1,4}\\d$"
or simpler \\d{2,5}$
. \b
(backspace symbol) to \\b
if you want it to represent word boundaryInteger.parseInt(String)
) and use proper combination of comparisons operators (<
, >
, <=
, >=
).Upvotes: 1