问题
I am looking for a regular expression to use in Swift to validate cardholder name for credit card. I am looking for a regEx which - - Has minimum 2 and maximum 26 characters - Accept dashes (-) and apostrophes (') only and no other special character - Capital and small alphabets and no numbers. - Should not start with a blank space.
I was using this - "^[^-\\s][\\p{L}\\-'\\s]{2,26}$"
but it only accepts dash (-) no apostrophe (')
Thanks in advance for any help.
回答1:
try with this regex
(?<! )[-a-zA-Z' ]{2,26}
see here
https://regex101.com/r/0UVvR1/1
回答2:
Guessing from your description, this is what you are looking for:
^[\p{L}'-][\p{L}' -]{1,25}$
Demo
A few remarks:
- you propbably do not want to allow all possible white-space chars
[\r\n\t\f\v ]
but just spaces. - you have to adjust the allowed lenght of the second string if you add a 1st group that does not include space and dash (since that group contributs an additional character).
- with
\p{L}
you allow any kind of letter from any language (which is good); otherwise use[a-zA-z]
if just want to allow the regular (ASCII) alphabet.
PS: Do not forget to escape the pattern properly: "^[\\p{L}'][\\p{L}' -]{1,25}$"
来源:https://stackoverflow.com/questions/49992102/regular-expression-in-swift-to-validate-cardholder-name