Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEX - Match special character anywhere in the string

I am not a regex guy, so I need a little help, just cant get my head around this...

I am writing registration page and using asp.net validators. I need a regex validator to match a special character anywhere within a word, so it will satisfy requirement which says that "ID must have at least one special character." Allowed characters are: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

Once again, I am a junior, and having problems with wrapping my head around this stuff at this moment...

For now I have

[\[\]\^\$\.\|\?\*\+\(\)\\~`!@#%&-_+={}'""<>:;, ]{1,}

but it matches numbers as well for some reason, and sometimes don't match characters in the beginning of the word...

So, please, if anyone could help me with that...

like image 333
Alex Avatar asked Feb 21 '12 22:02

Alex


1 Answers

You need to escape '-' and '!'

[\[\]\^\$\.\|\?\*\+\(\)\\~`\!@#%&\-_+={}'""<>:;, ]{1,}
like image 182
Dave Kasper Avatar answered Oct 06 '22 00:10

Dave Kasper