Problem with regex order
June 18, 2008 4:03 PM   Subscribe

RegExFilter: Can any regular expression ninjas help me with my this teensy problem?

I'm validating an input in cakephp as follow:

'rule' => array('custom', '/^[a-zA-Z\'&.\s]{1,40}$/')

It works great in that it allows letters, spaces, ampersand, apostrophe, period and numbers.

For example: Jim's Number 1 Tackle & Rod Shop. validates.

The problem arises when there is a number first, for example 2you delivery services will not validate.

I know it has something to do with the order in which the expression is written, but I can't figure it out. Anyone know what I'm doing wrong?

Thanks, as always.
posted by ReiToei to Computers & Internet (6 answers total)
Response by poster: Sorry, Jim's Number 1 Tackle & Rod Shop doesn't validate. I WANT it to validate!
posted by ReiToei at 4:08 PM on June 18, 2008

Response by poster: Uh, figured out... I'm sooo dumb. Can an admin delete this please? Thanks :)
posted by ReiToei at 4:13 PM on June 18, 2008

Missing a \d, eh? Easy to do...

You've validated that the rule excludes everything you want to exclude, right? because in some regexp formats . means 'any character' so your regexp would match everything.
posted by Mike1024 at 4:20 PM on June 18, 2008

Unless you don't want underscores, this shorter regex could do the trick:
^[\w\'&\. ]{1,40}         ^
I'd use a simple space rather than \s. You probably don't want to allow tabs or returns.
posted by Deathalicious at 4:48 PM on June 18, 2008

Sounds like you already have your answer, but for the archives -- if you use Emacs, the M-x re-builder tool is great for this stuff.
posted by trouserbat at 6:15 PM on June 18, 2008

Response by poster: Thanks for all the help guys! Will definitely check out that tool for Emacs.
posted by ReiToei at 3:37 AM on June 19, 2008

« Older Back me up, Scotty!   |   Who sung this song??? Newer »
This thread is closed to new comments.