iPhone 密码的正则表达式
我在创建正则表达式方面非常弱。所以我在这里。
我需要一个满足以下条件的正则表达式。
NSString *nameRegex =@"My RegEx";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
validationResult=[nameTest evaluateWithObject:password];
这些是条件
应包含字母和数字
最少 7 个字符 最多 32 个字符
无空格
没有符号
请帮忙..
I am pretty much weak in creating Regular Expression. So i am here.
I need a regular expression satisfying the following.
NSString *nameRegex =@"My RegEx";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
validationResult=[nameTest evaluateWithObject:password];
These are the conditions
It should contain letters and numbers
Minimum 7 characters Max 32 characters
No Space
No Symbols
Please Help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是你如何做到的!
/^[a-zA-Z0-9]{7,32}$/
不要忘记“开始到结束”标记。
根据记录,如果您希望字符串明确包含同时字母和数字,则无法使用一个正则表达式来做到这一点。 (事实上,从数学上来说你可以做到这一点,但是正则表达式会很大。)
只需运行上面的检查,然后还检查它是否包含一个字母:/[a-zA-Z]/
然后然后还测试它包含一个数字:/[0-9]/
这是最简单的方法。
Here's how you do it !
/^[a-zA-Z0-9]{7,32}$/
Don't forget the "beginning to end" markers.
For the record, if you want it the string to definitely include both letters and digits, there is no way to do that with one regex. (In fact, mathematically you could do it, but the regex would be enormous.)
Simply run the above check, and THEN ALSO CHECK that it contains a letter: /[a-zA-Z]/
And THEN ALSO TEST that it contains a digit: /[0-9]/
That's the easiest way.
作为一个非正则表达式专家,我会通过将标准分成单独的测试来解决这个问题,这样可以更容易地理解和更改,即多个 if 条件。
我相信这不能用一个正则表达式来完成,至少不能以一种可以理解的方式:)
As a non-regex expert, I would solve this by splitting up the criteria into separate tests which can then be easier understood and changed, ie multiple if conditions.
I believe this cannot be done with a single regex, at least not in an understandable fashion :)