对不起,如果这是一个愚蠢的问题,但它在过去的5天里一直让我精神错乱。
我正试图制作一个正则表达式模式以匹配爱尔兰汽车注册示例12-W-1234 '到目前为止,这就是我所拥有的:
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("12-WW-1"); input.add("12-W-223"); input.add("02-WX-431"); input.add("98-zd-4134"); input.add("99-c-7465"); for (String car : input) { if (car.matches("^(\\d{2}-?\\w*([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})$")) { System.out.println("Car Template " + car); } } } }当我检查那些在我的模式中有一个字母的 regs时,我的问题就出现了。 例如'12-ZD-1234' 。 ZD不是有效的县ID,但由于D有效,因此可以显示它。
任何帮助都会很棒。
我已经在一些网站上做过研究,包括这个和这个 。
这些网站有所帮助,但我仍然遇到了问题。
通过,我将更改模式以将所有输入更改为大写以减少我的代码的大小。 谢谢您的帮助
Sorry if this a dumb question but it's been driving me mental for the past 5 days.
I'm trying to make a regex pattern to match the Irish car registration example '12-W-1234' So far this is what I have:
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("12-WW-1"); input.add("12-W-223"); input.add("02-WX-431"); input.add("98-zd-4134"); input.add("99-c-7465"); for (String car : input) { if (car.matches("^(\\d{2}-?\\w*([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})$")) { System.out.println("Car Template " + car); } } } }My problems are coming up when it is checking regs that would have a single letter in the that is in my pattern. Eg '12-ZD-1234'. Where ZD isn't a valid county ID but since D is valid it allows it to be displayed.
Any help would be great.
I've already done research on a few websites including this and this.
These websites helped, but I'm still having my problems.
By the by, I'am going to change the pattern to change all inputs into uppercase to reduce the size of my code. Thanks for the help
最满意答案
除了其他人指出的\\w*之外,你还在滥用角色类 ( [...] )。 要实际使用交替 ( | ),请取出方括号:
^(\\d{2}-?(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})$以下是一些示例,向您展示角色类实际如何工作:
[abc]匹配单个字符, a , b或c 。 [aabbcc]等同于[abc] (忽略重复)。 [|]匹配管道符, 即允许符号。 [KK|kk|ww|WW|c|C|ce|CE ... ]最终等同于[K|wWcCeE ... ]因为再次忽略重复项。使用交替运算符( | )来执行您想要的操作是正确的,但您不需要使用字符类。
Besides the \\w* that others have pointed out, you're misusing character classes ([...]). To actually use alternation (|), take out the square brackets as well:
^(\\d{2}-?(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})$Here are some examples to show you how character classes actually work:
[abc] matches a single character, either a, b, or c. [aabbcc] is equivalent to [abc] (duplicates are disregarded). [|] matches a pipe character, i.e. symbols are allowed. [KK|kk|ww|WW|c|C|ce|CE ... ] ends up being equivalent to [K|wWcCeE ... ] because, again, duplicates are disregarded.You were correct to use the alternation operator (|) to do what you desired, but you didn't need to use character classes.
更多推荐
发布评论