[英]java string matching with regex
I have a specific key i need to match. 我有一个特定的钥匙我需要匹配。 The key is 10 set's of 4 separated by a dash.
密钥是10套,每套4个,用短划线隔开。 The combination can be a combination of letters or numbers.
该组合可以是字母或数字的组合。
Example. 例。
aa11-bb22-cc33-44dd-55ee-66ff-gg77-hh88-99ii-jj10 aa11-bb22-cc33-44dd-55ee-66ff-gg77-hh88-99ii-jj10
I just want to validate the pattern being 10 sets of 4 separated by a dash. 我只想验证模式为10组,每组4个,用破折号分隔。
Probably match this via regex, but I don't know how. 可能通过正则表达式匹配,但是我不知道如何匹配。
Any help would be appreciated. 任何帮助,将不胜感激。
^[a-zA-Z0-9]{4}(?:-[a-zA-Z0-9]{4}){9}$
Try this.See demo. 试试看。看演示。
http://regex101.com/r/kP8uF5/11 http://regex101.com/r/kP8uF5/11
([a-zA-Z0-9]{4}-){9}[a-zA-Z0-9]{4}
Here's the regex in action: http://regex101.com/r/xR1wV3/1 这是实际的正则表达式: http : //regex101.com/r/xR1wV3/1
Explanation: 说明:
[a-zA-Z0-9]
--> any character from a to z, A to Z and 0 to 9 [a-zA-Z0-9]
->从a到z,A到Z和0到9的任何字符 [a-zA-Z0-9]{4}
--> {4}
indicates exactly 4. So [a-zA-Z0-9]{4}
means exactly 4 characters [a-zA-Z0-9]{4}
-> {4}
表示正好4。因此[a-zA-Z0-9]{4}
表示正好4个字符 [a-zA-Z0-9]{4}
- --> add a dash (-) at the end to match for dash separators. [a-zA-Z0-9]{4}
-->在末尾添加破折号(-)以匹配破折号分隔符。 This should match with aa11-
for example aa11-
匹配 ([a-zA-Z0-9]{4}-)
--> put #3 into parenthesis ([a-zA-Z0-9]{4}-)
->将#3放在括号中 ([a-zA-Z0-9]{4}-){9}
--> add {9}
to the parenthesis to specify that this pattern repeat 9 times ([a-zA-Z0-9]{4}-){9}
->在括号中添加{9}
,以指定此模式重复9次 ([a-zA-Z0-9]{4}-)[a-zA-Z0-9]{4}
--> add a [a-zA-Z0-9]{4}
at the end to match the last set of 4 characters (same as #2 above) ([a-zA-Z0-9]{4}-)[a-zA-Z0-9]{4}
->在末尾添加[a-zA-Z0-9]{4}
以匹配最后一组4个字符(与上面的#2相同) You could do it like this: 您可以这样做:
(?!.*_)(\w{4}-){9}\w{4}
This makes use of the fact that \\w
means "any letter, number or underscore" and the negative look ahead prevents the underscore. 这利用了
\\w
表示“任何字母,数字或下划线”的事实,而否定的前瞻防止了下划线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.