正则表达式来匹配多个模式以任意顺序 [英] Regex to match multiple patterns in any order

查看:994
本文介绍了正则表达式来匹配多个模式以任意顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我验证了复杂的密码在ASP.NET MVC3应用程序。我的当前的要求是,它必须包含至少一个大写字母,一个小写字母,一个数字和不超过三个重复字符。我想概括这些数字,虽然,也可以增加一个条件,非字母数字字符。

I'm validating a password for complexity in an ASP.NET MVC3 app. My current requirements are that it must contain at least one upper case letter, one lower case letter, one digit and no more than three repeated characters. I'd like to generalise those numbers though, and also add a condition for non-alphanumeric characters.

在present,我验证服务器端而已,所以我可以使用一个正则表达式的每个条件调用Regex.IsMatch多次。我希望能够验证客户端也不过。因为不显眼的jQuery的验证将只允许一个正则表达式,我需要所有五个条件组合成一个单一的模式。

At present, I'm validating server-side only, so I'm able to call Regex.IsMatch multiple times using one regex for each condition. I want to be able to validate client-side too though. because unobtrusive jQuery validation will only allow one regex, I need to combine all five conditions into a single pattern.

我不知道很多时候还经常EX pressions,但我一直在做一点阅读最近。我可能失去了一些东西简单,但我不能找到一种方法,与多种模式一起的方式|或将它们。

I don't know much when it comes to regular expressions but I've been doing a bit of reading recently. I may be missing something simple but I can't find a way to AND multiple patterns together the way a | will OR them.

推荐答案

您可以做到这一点(在.net中)与几个的向前断言在一个单一的正则表达式:

You can do this (in .NET) with several lookahead assertions in a single regex:

^(?=.*\p{Lu})(?:.*\p{Ll})(?=.*\d)(?=.*\W)(?!.*(.).*\1.*\1)

将匹配,如果所有条件都满足。

will match if all conditions are true.

^                  # Match the start of the string
(?=.*\p{Lu})       # True if there is at least one uppercase letter ahead
(?=.*\p{Ll})       # True if there is at least one lowercase letter ahead
(?=.*\d)           # True if there is at least one digit ahead
(?=.*\W)           # True if there is at least one non-alnum character ahead
(?!.*(.).*\1.*\1)  # True if there is no character repeated twice ahead

请注意,该比赛是不会消耗字符串的任何字符 - 如果你想匹配操作返回你对匹配字符串,添加 * 在正则表达式的结尾。

Note that the match is not going to consume any characters of the string - if you want the match operation to return the string you're matching against, add .* at the end of the regex.

在JavaScript中,你不能使用统一code字符属性。因此,你可以使用

In JavaScript, you can't use Unicode character properties. So instead you could use

^(?=.*[A-Z])(?:.*[a-z])(?=.*\d)(?=.*\W)(?!.*(.).*\1.*\1)

这当然会只使用ASCII字符进行验证。如果这是你OK,挺好的。你可以去和增强的字符类,如 [A-ZÄÖÜÀÈÌÒÙÁÉÍÓÚ] 等等,等等,但你可能永远不会完全与此有关。在服务器端,如果你想验证,得到相同的结果,你必须指定 RegexOptions.ECMAScript 所以.NET正则表达式引擎的行为类似于JavaScript引擎(感谢艾伦·摩尔的察觉!)。

which will of course only use ASCII letters for validation. If that's OK for you, fine. You could go and augment the character classes like [A-ZÄÖÜÀÈÌÒÙÁÉÍÓÚ] etc. etc. but you would probably never be complete with this. On the server side, if you want the validation to yield the same result, you'd have to specify RegexOptions.ECMAScript so the .NET regex engine behaves like the JavaScript engine (thanks Alan Moore for noticing!).

这篇关于正则表达式来匹配多个模式以任意顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆