Monday, December 28, 2009

Regular expression to match only for any type of symbols.

Share at Facebook

I'm going to write a simple regex that will validate whether the provided string contains only symbols or not. No alphabet or numbers or spaces are allowed. Giving the expression using Perl, but it will work with any programming language as long as it PCRE.

my $str = "^%^";
if( $str =~ /^[!-\/:-@\[-`\{-~]+$/ ){
print "String contains only symbols.\n";

So the actual regex is ^[!-\/:-@\[-`\{-~]+$

No comments: