[英]Regex to include one (lowercase, uppercase, number, given special characters) in c# Azure function
I am writing a Azure function which is in c# language. 我正在用c#语言编写一个Azure函数。 Now I want to generate a password which contains one (lowercase, uppercase, number and given special character). 现在,我想生成一个包含一个密码(小写,大写,数字和给定特殊字符)的密码。
I am using Fare in Azure function for c# 我在Azure函数中使用Fare for C#
Azure function:- Azure功能:-
using Fare;
var regex=@"/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,30}$/";
var xeger = new Xeger(regex);
var result = xeger.Generate();
log.Info("result" + result);
Error:- 错误:-
2017-08-30T10:20:12.045 exceptionSystem.InvalidOperationException: state
at Fare.Xeger.Generate(StringBuilder builder, State state)
at Fare.Xeger.Generate()
at Submission#0.<Run>d__1.MoveNext() in D:\home\site\wwwroot\HttpTriggerCSharp1\run.csx:line 13
2017-08-30T10:03:28.989 Exception while executing function: Functions.HttpTriggerCSharp1. Microsoft.Azure.WebJobs.Script: One or more errors occurred. Fare: state.
Error on Line no 13 is var regex=... 第13行的错误是var regex = ...
Kindly help me to solve this. 请帮助我解决这个问题。
The exception comes from this part [A-Za-z\\d#$@!%&*?]
that characters after \\d
makeing exceptions. 异常来自此部分[A-Za-z\\d#$@!%&*?]
, \\d
之后的字符构成异常。 just change it to 0-9
if you want to match a digit; 如果要匹配数字,只需将其更改为0-9
; something like : [A-Za-z0-9#$@!%&*?]
or moving it to end like [A-Za-z#$@!%&*?\\d]
. 诸如: [A-Za-z0-9#$@!%&*?]
或将其移动到结尾[A-Za-z#$@!%&*?\\d]
。
But your complex regex will result after a long time ;). 但是,您的复杂正则表达式将在很长一段时间后产生;)。
Note: There are some rare results of using
\\d
inside[..]
;). 注意:在[..]
;)中使用\\d
有一些罕见的结果。
A better regex for your need, I think can be something like this: 满足您需求的更好的正则表达式,我认为可能是这样的:
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#$@!%&*?]).{8,}
Explanation: 说明:
(?=.*\d) => there is at least one digit
(?=.*[a-z]) => there is at least one lowercase character
(?=.*[A-Z]) => there is at least one uppercase character
(?=.*[#$@!%&*?]) => there is at least one special character
.{8,} => length is 8 or more
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.