tags:

views:

74

answers:

4
+4  Q: 

Regular Expression

I'm trying to get a regular expression that allows between 6 - 15 characters and can be a-zA-Z0-9 and the symbols above the numbers 1-8 on a keyboard.

Here's what I have but it doesn't work.

'/^[a-zA-Z0-9-_][\!\@\#\$\%\^&\*]{5,16}+$/'
+10  A: 

you have two different sets within brackets. Basically the expression says "1 of a-zA-Z0-9-_" followed by 5-16 of special characters. Combine them into the same set of brackets and you're all good.

Something like the following:

'/^[a-zA-Z0-9-_\!\@\#\$\%\^&\*]{5,16}+$/'
Rich
Works great. Thanks for explaining not only the correct answer but what was wrong with my answer.
Catfish
No prob. Regular expressions can get pretty out of hand, so I always try to read them and translate into plain language to check my work.
Rich
A: 

Try this:

^[\w-!@#$%^&*]{6,15}$

As \w means [a-zA-Z_0-9]

Rubens Farias
You need to escape most of the special characters.
macabail
Rubens Farias
A: 
'/^[a-zA-Z0-9-_!@#$%^&*]{5,16}$/'
pixelbeat
A: 
/^[\w\!\@\#\$\%\^&\]{6,15}*$/

That is what it would be in Perl.

macabail