Patrón RegEx dos letras seguidas de seis números

Please assist with the proper RegEx matching any 2 letters followed by any combination of 6 whole numbers.

These would be valid: 
RJ123456
PY654321
DD321234

These would not
DDD12345
12DDD123

preguntado el 03 de mayo de 12 a las 22:05

What about "ABC1234567"; might it ever occur? -

There wouldn't be a case for more than two letters or 6 numbers. -

Will the "whole numbers" always be the Western Arabic numerals 0-9, or will there ever be other digits? -

5 Respuestas

[a-zA-Z]{2}\d{6}

[a-zA-Z]{2} means two letters \d{6} significa 6 dígitos

If you want only uppercase letters, then:

[A-Z]{2}\d{6}

contestado el 03 de mayo de 12 a las 22:05

you forgot to start with ^, otherwise, if you have more than 2 letters, it will partially match. - fernando vieira

Podrías probar algo como esto:

[a-zA-Z]{2}[0-9]{6}

Here is a break down of the expression:

[a-zA-Z]    # Match a single character present in the list below
               # A character in the range between “a” and “z”
               # A character in the range between “A” and “Z”
   {2}         # Exactly 2 times
[0-9]       # Match a single character in the range between “0” and “9”
   {6}         # Exactly 6 times

This will match anywhere in a subject. If you need boundaries around the subject then you could do either of the following:

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

Which ensures that the whole subject matches. I.e there is nothing before or after the subject.

or

\b[a-zA-Z]{2}[0-9]{6}\b

which ensures there is a límite de palabras on each side of the subject.

As pointed out by @Phrogz, you could make the expression more terse by replacing the [0-9] para agendar una \d as in some of the other answers.

[a-zA-Z]{2}\d{6}

contestado el 03 de mayo de 12 a las 22:05

@Phrogz haha, its a matter of preference i guess, i usually use \d, but i dunno i felt a little different today. I'll put some more effort in and update it for you. - Robbie

@Fergus no worries, i haven't answered anything in a few days, and i was feeling fired up...lol - Robbie

Really great catch on ^ and $ without those 2 the regex is NOT going to do what OP expects or wants. - Douglas Adolph

I depends on what is the regexp language you use, but informally, it would be:

[:alpha:][:alpha:][:digit:][:digit:][:digit:][:digit:][:digit:][:digit:]

dónde [:alpha:] = [a-zA-Z] y [:digit:] = [0-9]

If you use a regexp language that allows finite repetitions, that would look like:

[:alpha:]{2}[:digit:]{6}

The correct syntax depends on the particular language you're using, but that is the idea.

contestado el 03 de mayo de 12 a las 22:05

[[:alpha:]] instead of [:alpha:] to find 2alpha and 6 digits [[:alpha:]]{2}[[:digit:]]{6} - stalin wesley

Depending on if your regex flavor supports it, I might use:

\b[A-Z]{2}\d{6}\b    # Ensure there are "word boundaries" on either side, or

(?<![A-Z])[A-Z]{2}\d{6}(?!\d) # Ensure there isn't a uppercase letter before
                              # and that there is not a digit after

contestado el 03 de mayo de 12 a las 22:05

Everything you need here can be found in esta guía de inicio rápido. A straightforward solution would be [A-Za-z][A-Za-z]\d\d\d\d\d\d or [A-Za-z]{2}\d{6}.

If you want to accept only capital letters then replace [A-Za-z] con [A-Z].

Respondido el 30 de Septiembre de 15 a las 16:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.