Regular expression in Swift to validate Cardholder name

时光毁灭记忆、已成空白 提交于 2019-12-13 08:14:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!