Regex to allow A-Z, - and '

后端 未结 6 2039
情歌与酒
情歌与酒 2021-01-16 21:25

I am trying to get this Regex to work to validate a Name field to only allow A-Z, \' and -.

So far I am using this which is working fine apart from it wont allow an

6条回答
  •  被撕碎了的回忆
    2021-01-16 21:42

    Your code already does what you want it to:

    ... prints:

    Valid: Jim
    Valid: John
    Valid: O'Toole
    Valid: one-two
    Valid: Daniel'Blackmore
    Invalid:  Jim
    Invalid: abc123
    Invalid: $@#$%@#$%&*(*&){}//;;
    

    The single quote does not have any special meaning in regular expressions so it needs no special treatment. The minus sign (-), when inside [], means range; if you need a literal - it has to be the first or last character, as in your code.

    Said that, the error (if any) is somewhere else.

提交回复
热议问题