简体   繁体   English

Swift4 中的正则表达式来验证全名

[英]Regex in Swift4 to validate fullname

I'm looking for a regular expression to use in Swift to validate the full name.我正在寻找在 Swift 中使用的正则表达式来验证全名。 I'm looking for a regex that has a minimum of 2 and a maximum of 100 characters - Accept Accents, dashes (-) and apostrophes (') and no other special character - Upper and lower case letters and no numbers.我正在寻找一个至少有 2 个和最多 100 个字符的正则表达式 - 接受重音、破折号 (-) 和撇号 (') 并且没有其他特殊字符 - 大小写字母和没有数字。 - It must not start with a blank space and cannot have more than 1 blank space between the names. - 它不能以空格开头,并且名称之间的空格不能超过 1 个。

I was using this - "(?mi)^[A-Za-zÀ-ú](??(:..*\?\/\ ){2})(?:(.?.* ){10})(..,*\.[az])[A-Za-zÀ-ú. '-]{5,99}[A-Za-zÀ-ú]$" but is accepting more than one space and some names that I shouldn't我正在使用这个 - "(?mi)^[A-Za-zÀ-ú](??(:..*\?\/\ ){2})(?:(.?.* ){10})(..,*\.[az])[A-Za-zÀ-ú. '-]{5,99}[A-Za-zÀ-ú]$"但接受多个空格和一些名称我不应该

link https://regex101.com/r/493eLF/12链接https://regex101.com/r/493eLF/12

Thank you in advance for any help.预先感谢您的任何帮助。

You might use a repeating group that starts with a single space followed by repeating 1+ times the listed characters in the character class.您可以使用以单个空格开头的重复组,然后将字符 class 中列出的字符重复 1 次以上。

To verify the string length, you could use a positive lookahead at the start of the string.要验证字符串长度,您可以在字符串的开头使用正向前瞻。

If there should be no trailing spaces allowed, you can omit the * at the end right before the $如果不允许有尾随空格,则可以在$之前省略末尾的*

If the single uppercase chars are also allowed, you can change the quantifier for the first [A-Za-zÀ-ú.'-]+ to *如果也允许使用单个大写字符,您可以将第一个[A-Za-zÀ-ú.'-]+的量词更改为*

^(?=.{2,100}$)[A-Za-zÀ-ú][A-Za-zÀ-ú.'-]+(?: [A-Za-zÀ-ú.'-]+)* *$

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM