简体   繁体   English

用于验证2个字母数字的正则表达式

[英]regular expression to validate 2 alphanumerics

I have the follow pattern to validate a string, it has to validate 4 letters , 6 numbers , 6 letters and 2 alphanumerics , but with my current pattern I cant get a valid test 我有以下模式来验证字符串,它必须验证4个字母6个数字6个字母2个字母数字 ,但是使用我当前的模式,我无法获得有效的测试

Pattern.compile("[A-Za-z]{4}\\d{6}\\w{6}\\[A-ZÑa-zñ0-9\\- ]{2}");

I think my pattern it's wrong, because I'm not shure about this [A-ZÑa-zñ0-9\\\\- ]{2} 我认为我的模式是错误的,因为我对这个[A-ZÑa-zñ0-9\\\\- ]{2}

Can you please help me? 你能帮我么?

You can use pattern: 您可以使用模式:

^[a-zA-Z]{4}[0-9]{6}[a-zA-Z]{6}[a-zA-Z0-9]{2}$

Check it live here . 在这里查看它。 In your expression you are using \\w+ , which does not only match digits and alphabetic characters, but also underscores _ . 在表达式中,您正在使用\\w+ ,它不仅匹配数字和字母字符,而且还下划线_

A few things off on your regex. 正则表达式上有一些问题。

  • You have extra backslashes in your digit and word matching. 您的数字和单词匹配中有多余的反斜杠。 Change from \\\\d to \\d and \\\\w to \\w . \\\\d更改为\\d ,从\\\\w更改为\\w

  • The \\\\ is not needed. 不需要\\\\

  • Your end regex is invalid syntax. 您的最终正则表达式语法无效。 Just remove the "\\\\- " bit. 只需删除"\\\\- "位。

You can also slim down your initial part to be \\w instead of [A-Za-z] . 您也可以将初始部分减为\\w而不是[A-Za-z] So, you're new regex should look like: 因此,您是新的正则表达式应如下所示:

"\w{4}\d{6}\w{6}[A-ZÑa-zñ0-9]{2}"

That is if you're okay with the only non-ascii characters being Ñ and ñ in your last two alphanumerics. 那就是您可以接受的情况,在最后两个字母数字中唯一的非ASCII字符是Ñ和ñ。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM