Skip to main content
Inspiring
August 11, 2011
Answered

Need help with regular expressions

  • August 11, 2011
  • 2 replies
  • 784 views

I don't know why regular expressions are not working in my application.  I've tried it several ways and it's not working.  Ultimately, I want to allow any letter, number, underscore or dash, but I cannot get a basic regular expression working (sometimes it accepts what it's not supposed to and sometimes it accepts nothing).  I'm using ColdFusion 9,0,1,274733 Enterprise on Linux.

<CFINPUT TYPE="Text" MESSAGE="Please enter a Reservation ID.  Only letters, numbers, dashes and underscores are allowed (no spaces or special characters)." NAME="usereventID" REQUIRED="Yes" SIZE="30" VALIDATE="regular_expression" PATTERN="[[:word:]]+">

I tried:

PATTERN="[[:word:]]+"

PATTERN="[[:word:]]"

PATTERN="[:word:]"

PATTERN=":word:"

PATTERN="[[A-Z][a-z][0-9]-_]+"

PATTERN="\w"

and none of these work correctly.  Any ideas on what could be wrong?

This topic has been closed for replies.
Correct answer -__cfSearching__-
PATTERN="\w"

I am not a regex guru, but that seemed to came the closest.  Do not forget to add a starts with "^" and ends with "$" to ensure the string contains only those characters.  Add in the dash and it should do the trick.

                     PATTERN="^[\w\-]+$"

2 replies

Inspiring
August 11, 2011
I tried:

PATTERN="[[:word:]]+"

PATTERN="[[:word:]]"

PATTERN="[:word:]"

PATTERN=":word:"

PATTERN="[[A-Z][a-z][0-9]-_]+"

PATTERN="\w"

and none of these work correctly.  Any ideas on what could be wrong?

Yep.

1) JavaScript regexes don't support posix regex character class constructs like [:word:]

2) the penultimate one is just malformed.  You don't need the inner square brackets.

3) Also with that one, you need to read the note about using hyphens in character sets, here: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html (these are CF regex rules, not JS, but the same applies.

4) The last one should match a single alpha-numeric character or an underscore.  But not a hyphen/dash.

--

Adam

-__cfSearching__-Correct answer
Inspiring
August 11, 2011
PATTERN="\w"

I am not a regex guru, but that seemed to came the closest.  Do not forget to add a starts with "^" and ends with "$" to ensure the string contains only those characters.  Add in the dash and it should do the trick.

                     PATTERN="^[\w\-]+$"