0

I would like to know how to write a pattern for a regex in Razor/MVC3. I have been searching how to write the pattern but since I didn't know anything about MVC/Razor I only found a regex pattern for

characters only
^[a-zA-Z]+$

numbers only
^[0-9]*$

character and numbers only
^\w+$

email address. 
/\S+@\S+\.\S+/

Those patterns I found are not the one I am looking for. I would like to create a pattern which will exempt these characters "&';,<>. I thought of one way doing it. Like this:

^[a-zA-Z0-9_!#$%()-=~^|+and so on..]*$

Do I have to list all the characters just to exempt those? Or there is more an efficient way of doing it?

Elise van Looij
  • 4,162
  • 3
  • 29
  • 52
Rii
  • 1
  • 2
  • Like this? `^[^"&';,<>]*$` – Simon MᶜKenzie Feb 29 '16 at 03:23
  • @SimonMᶜKenzie Doesn't that accept only those characters in your string? I want to exempt those. – Rii Feb 29 '16 at 03:28
  • The caret inside the character class indicates that the match should exclude the characters within the class: http://www.regular-expressions.info/charclass.html#negated – Simon MᶜKenzie Feb 29 '16 at 03:31
  • Ah. I see. Thank you. I'll try that if it works in my site. :) – Rii Feb 29 '16 at 03:47
  • `[RegularExpression(@"^[^" & ';,<>]*$")]` Using this is not working because this can't be a whole string because of the " that is included in the excluded characters. @SimonMᶜKenzie – Rii Feb 29 '16 at 04:45
  • Then you need to either use a verbatim string or escape the quote, e.g. `@"^[^"" & ';,<>]*$"` or `"^[^\" & ';,<>]*$"`. – Simon MᶜKenzie Feb 29 '16 at 04:48
  • Oh I see. Thank you very much!! – Rii Feb 29 '16 at 05:20
  • @SimonMᶜKenzie please see the update. :) Thank youuuu. Please write it as the answer below. :D – Rii Mar 01 '16 at 07:26
  • Possible duplicate of [Regex - Does not contain certain Characters](http://stackoverflow.com/questions/4105956/regex-does-not-contain-certain-characters) – Simon MᶜKenzie Mar 02 '16 at 03:24

2 Answers2

0

(Posted on behalf of the OP).

I have used this @"^[^"" & ';,<>]*$" as suggested by Simon in the comments, and it worked.

halfer
  • 19,824
  • 17
  • 99
  • 186
-1

Have you tried this ?

 "[<>'"/;&]"