Hafiz H
Hafiz H

Reputation: 399

Regex.IsMatch for specific special characters in c#

I am new to Regex, I know this is a simple question but couldn't find a solution

I have a set of special characters (&, !, $, |) and a string. I just need to check whether the string contains any of these special characters.

I tried with this but couldn't make it out.

string _validateText = ".*&!$|.*";
string _myString = Value;
if(Regex.IsMatch(_myString, _validateText))
{ 
   //My Code  
}

I ignore all the other special characters. Tried with multiple String.Contains but need to do in Regex. Please help

Upvotes: 1

Views: 2110

Answers (1)

vks
vks

Reputation: 67968

Use a character class with anchors

^.*[&!$|].*$

Upvotes: 1

Related Questions