Java正则表达式模式匹配(爱尔兰汽车注册)(Java regex pattern matching (Irish car registration))

编程入门 行业动态 更新时间:2024-10-25 04:14:05
Java正则表达式模式匹配(爱尔兰汽车注册)(Java regex pattern matching (Irish car registration))

对不起,如果这是一个愚蠢的问题,但它在过去的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.

更多推荐

本文发布于:2023-07-22 12:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爱尔兰   模式   汽车   正则表达式   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!