Skip to main content
June 27, 2008
Question

Regular Expression Help

  • June 27, 2008
  • 3 replies
  • 416 views
Hello,

I am trying to validate an input field using Regular Expression. I have gotten regexp to work great for me in the past, including things like dates, email addresses and such, but this one is kicking my butt.

From what I have read, this pattern should work:

[CGNPRST][AELMNUS][1-6]?[A-Z0-9]{2,2}

(4 or 5 characters, with the middle 1-6 optional)

This should match items like:
CL01
CL0F
SS0F
SM20
SMTU
TE11
PA3IT
PACH

And should not match:
PA9IT
PAIT9

I thought the question mark made the previous character optional (match 0 or 1 time).

If I take the ? out, then I can get PA3IT to work and PA9IT does not, but neither does PACH or SS0F.

Any suggestions?

Doug Jessee
(At CFUnited 2008 I learned too much, now I have to change everything.)
This topic has been closed for replies.

3 replies

June 30, 2008
A former coworker figured it out.

I simply needed to put boundaries at the beginning and end.

^[CGNPRST][AELMNUS][1-6]?[A-Z0-9]{2,2}$

By simply adding the carrot (^) and dollar sign ($), it works as expected.

LDJ
BKBK
Community Expert
Community Expert
June 29, 2008
Did you once use {2,3} in place of {2,2}? Just a thought.

June 29, 2008
Hello and thanks for responding.

I did not use {2,3}.

What I am trying to get to work is a Regular Expression that matches the items in 'should' and not match the 'should not'.

I have tried several variations, but from what I have read, the regular expression I included should match those patterns. In actual usage, it does not, atleast not in ColdFusion v7.0.2.

The regular expression I included will correctly match things like PA3IT, but will also match things I dont want it to, like PA9IT. It also does not match things like SS0F.

I say it does or does not work from actually trying it on a coldfusion server.

Any help or suggestions as to changing the regular expression would be greatly appreciated.

LDJ
BKBK
Community Expert
Community Expert
June 29, 2008
[CGNPRST][AELMNUS][1-6]?[A-Z0-9]{2,2}
... should not match:
PA9IT
PAIT9


It doesn't. It matches PA9I and PAIT, but not PA9IT or PAIT9.

It also matches PA3IT, PACH and SS0F, as you require.