正则表达式在c#Azure函数中包含一个(小写,大写,数字,给定特殊字符)
问题描述:
我正在编写一个使用c#语言的Azure函数。现在我想生成一个包含一个(小写,大写,数字和给定特殊字符)的密码。正则表达式在c#Azure函数中包含一个(小写,大写,数字,给定特殊字符)
我使用Fare在Azure的功能为C#
Azure的功能: -
using Fare;
var [email protected]"/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#[email protected]!%&*?])[A-Za-z\d#[email protected]!%&*?]{8,30}$/";
var xeger = new Xeger(regex);
var result = xeger.Generate();
log.Info("result" + result);
错误: -
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.
错误线路上无13 VAR正则表达式= ...
请帮我解决这个问题。
答
来自[A-Za-z\d#[email protected]!%&*?]
这个部分的例外来自于\d
作为例外的字符。如果您想匹配一个数字,只需将其更改为0-9
;例如:[A-Za-z0-9#[email protected]!%&*?]
或将其移动到像[A-Za-z#[email protected]!%&*?\d]
那样结束。
但你复杂的正则表达式会在很长时间后产生;)。
Note: There are some rare results of using
\d
inside[..]
;).
你需要一个更好的正则表达式,我想可能是这样的:
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#[email protected]!%&*?]).{8,}
说明:
(?=.*\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
(?=.*[#[email protected]!%&*?]) => there is at least one special character
.{8,} => length is 8 or more
什么类型的模式(针对正则表达式引擎)做Xeger接受?你的模式包含lookaheads,我认为这些不支持。尝试'var regex = @“^ [A-Za-z \ d#$ @!%&*?] {8,30} $”;',它是否按预期产生结果? –
*发生一个或多个错误*那么错误是什么?异常是否有内部异常? – DavidG
@DavidG我只看到一个或多个错误。 – karan