-1

I am trying to get a regex working but cannot get it right. I am trying to write a regex c# that would allow all special chars except comma, single quotes & double quotes. But cant get it to work.

I have the below code:

 Regex alphaNumericRegex = new Regex(@"^[\w]*$", RegexOptions.IgnoreCase);

I tried using

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

where I thought the first negate [] set is for the chars which I dont want to allow.

But this does not work. There must be a simpler way to do this.

Sorry I know there are many regex posts around but somehow I cant get mine to work even after reading them.

Would appreciate inputs.

Brenda
  • 510
  • 3
  • 13
  • 36
  • 4
    Do you mean to write `@"^[^""',]*$"` matching a string consisting of any 0+ chars other than double and single quotes and commas? – Wiktor Stribiżew Jan 02 '19 at 19:04
  • @WiktorStribiżew You should post this as the answer. – Lews Therin Jan 02 '19 at 19:07
  • Karen, what is the trouble here? Do you want to say you just failed to use a double quote in the string literal? I guess that was the main trouble with getting at the right expression. – Wiktor Stribiżew Jan 02 '19 at 19:22
  • Yes thanks for this wiktor. This works fine. I had missing double quotes. Also I thought we had to use all the chars list which we also want to allow, which is not the case though. – Brenda Jan 02 '19 at 19:40

1 Answers1

-1

By changing your regex to the following, you will allow all characters except commas, single quotes and double quotes.

Regex alphaNumericRegex = new Regex(@"^[^""',]*$", RegexOptions.IgnoreCase);
JammoD
  • 419
  • 5
  • 15