• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

I've never understood regular expressions [RESOLVED]

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

rebelwarlock

Member
Joined
Dec 13, 2004
So I want to use preg_match() to detect a valid international number. The use of parenthesis and hyphens should be valid, but not mandatory. Basically, I want to throw up an error if the user enters anything other than digits, hyphens, or parenthesis. Any thoughts?
 
Last edited:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski

That's what I think when I hear regular expressions. I don't know it well enough to give you one off the top of my head but here is a site that helps me when I have to use them. It's got a regex tester as well as some guides on how to make them.

http://regexlib.com/RETester.aspx
 
Also check this out

PHP Regular Expressions


was thinking it should work something like [a-zA-Z0-9\-\(\)]

I haven't done php in a while, but that statement should be any lower or upper case letters, any numbers, the minus sign(hyphen) or open/close parens..please don't quote me though!
 
Regular expressions are the shiz.
Basically, I want to throw up an error if the user enters anything other than digits, hyphens, or parenthesis. Any thoughts?
if input matches [0-9\-\(\)]+
// it's all good
else
// it is not so good
 
Last edited:
Regular expressions are the shiz.

if input does't match [0-9\-\(\)]+
// it's all good
else
// it is not so good

Yup, that did it! It's been about two years since I've had to do PHP (or any coding, really). Teaching made me slow. Thanks for the links too, they're going to help since I've been asked to work on form validation.
 
Two nights ago, a misplaced wildcard in a regex in a script I was writing almost wiped out a .git repository.

/am terrified by regexes, especially in scripts ><
 
Back