stringasp.net-corerazor-pagesnsregularexpression

Regular Expressions Razor Pages ASP.NET core @"^[A-Z]+[a-zA-Z""'\s-]*$"?


What does the '\s-]*$" at the end of @"^[A-Z]+[a-zA-Z""'\s-]*$" stand for? Am I right saying it means no white space and numbers allowed? I understand the start of it, just the last bit is confusing me. And it is the same for @"^[A-Z]+[a-zA-Z0-9""'\s-]*$" Thanks in advance


Solution

  • Anything inside of square brackets is matching a character or set of characters. So here. I recommend using a tool like Regexr to help you narrow down how it works with whatever input you can provide it.

    But I'll break down what this means.

    ^[A-Z]+[a-zA-Z""'\s-]*$
    

    First of all, ^ means "the beginning of the input string" and $ means "the end of the input string" - So everything in between those characters is what is being matched

    There are two character sets being matched

    First is [A-Z]+ which means "match any capital letter from A to Z" and then the + at the end means to match the previous set at least one time, but as many times as possible

    Next is [a-zA-Z""'\s-]* which is another set that matches:

    Finally the * at the end means "match the previous set zero or more times, as many times as possible