Match at least one non Special Character Using Regular Expression

 

How to match at least one non special character using Regular Expression

Today, one of my colleague came to me & asked to validate a string which should contain at least one non special character.

I think when there is a question of string validation by processing character set no one can beat Regular Expression so we started implementing it.

You might known about it already but still when I look this helpful information the first thing comes to my mind is to share.

As per your requirements you can get help from the website & design your own regular expression. So…. here comes the link.

Website URL: http://www.regextester.com/

Now, I will Explain the below regular Expression.

Regular Expression which match the string if it contain at least one non special character:

^ (\W*\w+.*)$

Expression

Description

^

Match the beginning of a line

$

Match the end of a line

()

Capture matched substring

\W

Matches a non-word character

\w

Matches a word character, same as [a-zA-Z_0-9]

*

Zero or more matches

+

One or more matches

.

Matches any character except \n

So, now you know individual meaning of expression used in the above regular expression.

For getting more understanding on the regular expression refer this url: http://www.developer.com/net/cplus/article.php/10919_3485636_2.

Let’s, understand how this all work together for  validate string.

^ () $ :- This will match the entire string with Expression given inside the Bracket.

\W*\w+.*

\W* :- This will match the zero/more occurrence of the non special character in the string.

For example, if u give sting like #$%@ -this will consider as valid.

\w+ :- Now, this will ensure that at least one non special character should be in string.

.* :- This will match any character except \n in the string.