简体   繁体   中英

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

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}

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 _ .

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 .

  • 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] . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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