0

I know from a tutorial that the regular expression ^[A-Z]+[a-zA-Z''-'\s]*$ matches a string of letters not including whitespace. I know that \s means whitespace. Why when using this in validation for a field in MVC do I have to put[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")] ?

What is the meaning of the two single quotes and the single quote round the hyphen before the \s?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
TraceyB
  • 21
  • 4
  • 1
    That's the same string you found in the tutorial. The quotes have nothing to do with `\s`. The pattern itself does *not* return non-whitespace strings. It matches strings that start with capital letters and contain any letter, quote, dash or *space*. It will match `Aa bc'd`. Where did you find it? – Panagiotis Kanavos Nov 08 '17 at 16:50
  • 1
    Where is this tutorial? `^[A-Z]+[a-zA-Z''-'\s]*$` looks to be identical to `^[A-Z]+[a-zA-Z'\s]*$` and I am wondering if they in fact meant the two single quotes to be a double quote. `"-'` would then mean the range of characters from " to ' (which is "#$%&'). – Chris Nov 08 '17 at 16:50
  • I agree with @Chris, it's probably supposed to be a double quote. From my searching, this exact (mistyped?) regex string appears in *ASP.NET MVC with Entity Framework and CSS* By Lee Naylor, page 70. According to the author, the regex specifies that the "first character must be an upper case letter followed by a repetition of letters and spaces." – C. Helling Nov 08 '17 at 16:58
  • Good sleuthwork. It also appears we've already had this question (near enough) on this site: https://stackoverflow.com/questions/33173272/intention-of-regular-expression – Chris Nov 08 '17 at 17:03

0 Answers0