0

I have an aspx:memo control in asp.net page. The control has a regular expression validator. If the text inserted in the memo is different than the regular expression then the validator triggers an error. So I would like to accept every character except these:

1) -- (double hyphen)
2) // (double slash)
3) ' (Single quote)
4) \\ (double backslash)
5) ^ (Caret)
6) ; (Semicolon)

So far I have created this expression:

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

I have put inside every character that I accept in the memo. So the issue is that with this expression I accept slashes and backslashes or hyphens even if they are single or more. How can I disallow the double slashes or hyphens but allow single ones.

I have already lost a lot of time for this. Your help would be greatly appreciated.

Thank you in advance.

Kate10
  • 53
  • 6

2 Answers2

3

^((?!--|\/\/|'|\\\\|\^|;).|\w)*$

Breakdown

(?! ) being the negative look ahead meaning that anything within (separated by |) will cause the validation to fail

\/\/ being // with escaped characters

\\\\ being \\ with escaped characters

. allows any character (except whitespace)

\w allows whitespace

Community
  • 1
  • 1
Joshua Loader
  • 384
  • 1
  • 5
1

Try this one:

(?<!\-)(?<!\\)(?<!\/)[^;\^'](?!\-)(?!\\)(?!\/)

Explanation

(?<!\-)(?<!\\)(?<!\/) => Do not match \ or / or ' before the blacklist.

[^;\^'] => Our blacklist. Do not match ; or ' or ^, match everything else.

(?!\-)(?!\\)(?!\/) => Dot not match \ or / or ' after the blacklist.

So we allow everything but ; and '. But only, if there is no \ or / or ' in front or after the character. This means single occurances of these characters are allowed.

EDIT:

This matches only single characters. Better use the accepted answer.

Marcel Kirsche
  • 445
  • 4
  • 11