[英]Swift, phone number regex
Hopefully a simple one, 希望是一个简单的,
I need aa limit of 8 numbers, the user need to write 8 number no more or less. 我需要限制8个数字,用户需要写入8个数字不多或少。
For now this is my code: 现在这是我的代码:
telefonRegex = "^(?=.*[0-9])$"
But it is not working, I just heard about regex fyi. 但它没有用,我刚刚听说正则表达式。
Your current regex never matches a string because it requires to start matching at the start of the string ( ^
), then makes a forward check to require a digit ( [0-9]
) to appear after any 0+ chars other than line break chars ( .*
) and then tries to match the end of the string right after the beginning - tha is, it matches an empty string but also requires at least 1 digit in it. 您当前的正则表达式从不匹配字符串,因为它需要在字符串的开头( ^
)开始匹配,然后进行前向检查以要求在除了换行符之外的任何0+字符之后出现一个数字( [0-9]
) chars( .*
)然后尝试在开头之后匹配字符串的结尾 - 也就是说,它匹配一个空字符串,但也需要至少1位数字。
You may just use 你可以使用
let telefonRegex = "^[0-9]{8}$"
or 要么
let telefonRegex = "\\A[0-9]{8}\\z"
to match a string that only consists of 8 digits. 匹配仅包含8位数的字符串。
Details 细节
^
- start of string (may be replaced by \\\\A
in the string literal) ^
- 字符串的开头(可以用字符串文字中的\\\\A
替换) [0-9]{8}
- exactly 8 occurrences of any digit [0-9]{8}
- 任何数字恰好出现8次 $
- end of string (to make sure the very end of string is matched, use \\\\z
in the string literal). $
- 字符串结尾(为了确保字符串的末尾匹配,在字符串文字中使用\\\\z
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.