Operating System - HP-UX
1829750 Members
1429 Online
109992 Solutions
New Discussion

Need a regexp to match exactly 2 alpha & 4 digits in that sequence

 
SOLVED
Go to solution
Jaris Detroye
Frequent Advisor

Need a regexp to match exactly 2 alpha & 4 digits in that sequence

I am trying to find a regular expression that matched the form 'xxyyyy' then x=alpha & y=digit. There must be 2 letters & 4 numbers in that sequence no more no less to match.

Reading the man page 'regexp (5)' is making my brain hurt. I have tried using collating sequences but getting the exact sequence is tough.
7 REPLIES 7
H.Merijn Brand (procura
Honored Contributor

Re: Need a regexp to match exactly 2 alpha & 4 digits in that sequence

For perl that would be:

m/[A-Z]{2}\d{4}/

Posix:

/[[:alpha:]]{2}[[:digit:]]{4}/


From perl:

l1:/u/usr/merijn 102 > perl -nle'print /[[:alpha:]]{2}[[:digit:]]{4}/?"":"not ","OK"'
aa2345
OK
a45678
not OK
abc3456
OK
AbAb42
not OK
a1
not OK

not OK
blah!2345
not OK
blah1234
OK
^[[A
not OK
blah12345
OK


if you need it `anchored', in perl that would be:

/\b[[:alpha:]]{2}\d{4}\b/

Note that above will honour locale settings in perl
Enjoy, Have FUN! H.Merijn
Adam J Markiewicz
Trusted Contributor

Re: Need a regexp to match exactly 2 alpha & 4 digits in that sequence

Isn't it [a-zA-Z]{2}[0-9]{4} what you are looking for?
But this is in Extended Regular Expression form.
I belive that in not extended it would be something, like:
[a-zA-Z]\{2\}[0-9]\{4\}

Notice: NO SPACES
I hope it helped. Good luck.

Adam
I do everything perfectly, except from my mistakes
John Dvorchak
Honored Contributor

Re: Need a regexp to match exactly 2 alpha & 4 digits in that sequence

or the long version for you if you like to type

[a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9]

If it has wheels or a skirt, you can't afford it.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Need a regexp to match exactly 2 alpha & 4 digits in that sequence

Please note that however nice [A-Z] looks (and types), it is NOT locale safe. We're lucky not to work in EBCDIC, where it will fail horribly.

[[:alpha:]] is locale-safe. All perl shortcuts are locale-safe (\w, \d, ...)
Enjoy, Have FUN! H.Merijn
Jaris Detroye
Frequent Advisor

Re: Need a regexp to match exactly 2 alpha & 4 digits in that sequence

/[[:alpha:]]{2}[[:digit:]]{4}/

seems to work well enough for my needs, but I noticed in the test set you ran that it also matched sequences of more than two letters and 4 numbers e.g. is said 'blah12345' was good when in fact it is outside the target pattern. Luckly this will work with the data I have...thanks.
James R. Ferguson
Acclaimed Contributor

Re: Need a regexp to match exactly 2 alpha & 4 digits in that sequence

Hi:

To enable your last requirement, try:

/[[:space:]][[:alpha:]]{2}[[:digit:]]{4}[[:space:]]/

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Need a regexp to match exactly 2 alpha & 4 digits in that sequence

Hi (again):

This covers more cases:

/(^|[[:space:]])[[:alpha:]]{2}[[:digit:]]{4}($|[[:space:]])/

Regards!

...JRF...