tags:

views:

65

answers:

4

Hey can anyone whip up a regex that i could use to validate strings containing only alphanumeric characters along with ' ', '-', '_', and '.' ?

Thanks

+5  A: 
/^[A-Za-z0-9 _.-]+$/

or, where supported,

/^[\w .-]+$/
chaos
+1  A: 

Try this:

^[a-zA-Z0-9 \-_.]+$
Gumbo
+3  A: 
/^[\w. -]*$/

The \w predefined character class includes alphanumeric characters and underscores, and is shorter to type than a-zA-Z0-9_ Also, depending on whether you want to allow empty strings or not, you'll want to use either * or +.

Amber
Assuming, of course, your regex flavor supports it.
chaos
Correct. But the vast majority do.
Amber
I've never come across one that doesn't support shorthand character classes. Example?
CptSkippy
Uhm, POSIX and GNU BRE and ERE?
chaos
POSIX "Basic Regular Expressions"
sylvanaar
Good to know, I don't get out much. :)
CptSkippy
A: 

@chaos or anyone else: I am just curious, where isn't \w and \d stuff supported. I use them in Perl and Ruby. I had the idea that they are supported by every language.

vulcan_hacker