Objective c 如何在objective c中生成字母数字的正则表达式
如何生成一个包含char和number的正则表达式。如果只是字母或数字,它应该返回false 例如: 我的正则表达式:Objective c 如何在objective c中生成字母数字的正则表达式,objective-c,swift,regex,Objective C,Swift,Regex,如何生成一个包含char和number的正则表达式。如果只是字母或数字,它应该返回false 例如: 我的正则表达式: [a-z]|[0-9] 使用 或者,一个可能更高效的版本: ^(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])[0-9A-Za-z]+$ 看 消除: NODE EXPLANATION --------------------------------------------------------------
[a-z]|[0-9]
使用
或者,一个可能更高效的版本:
^(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])[0-9A-Za-z]+$
看
消除:
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
[A-Za-z] any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[0-9A-Za-z]+ any character of: '0' to '9', 'A' to 'Z',
'a' to 'z' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
感谢您将问题更新为更具体的内容。您可以使用
^(?[a-z]+\d+\d+[a-z]+)$
(如果还需要大写变体,请将[a-z]
更改为[a-zA-z]
)。或者,如果只需要确保每个字符集都存在(但允许正则表达式匹配任何字符,如
),则可以使用^(?=[^a-z]*[a-z])(?=\D*\D)
*?
无效。相反,使用[^a-zA-Z]*
和\D*
(并考虑\n
,使用以下内容)<代码>^(?=[^a-zA-Z\n]*[a-zA-Z])(?:[^\d\n]*[0-9])[0-9A-zA-Z]+$比您当前的模式更有效。
^(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])[0-9A-Za-z]+$
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
[A-Za-z] any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[0-9A-Za-z]+ any character of: '0' to '9', 'A' to 'Z',
'a' to 'z' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string