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

Need help with regular expressions

Explorer ,
Aug 11, 2011 Aug 11, 2011

Copy link to clipboard

Copied

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?

TOPICS
Advanced techniques

Views

642

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

correct answers 1 Correct answer

Valorous Hero , Aug 11, 2011 Aug 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\-]+$"

Votes

Translate

Translate
Valorous Hero ,
Aug 11, 2011 Aug 11, 2011

Copy link to clipboard

Copied

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\-]+$"

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
LEGEND ,
Aug 11, 2011 Aug 11, 2011

Copy link to clipboard

Copied

LATEST
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

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