• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Regular Expression Help

Guest
Jun 27, 2008 Jun 27, 2008

Copy link to clipboard

Copied

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.)
TOPICS
Advanced techniques

Views

362

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2008 Jun 28, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2008 Jun 28, 2008

Copy link to clipboard

Copied

Did you once use {2,3} in place of {2,2}? Just a thought.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 29, 2008 Jun 29, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 30, 2008 Jun 30, 2008

Copy link to clipboard

Copied

LATEST
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation