简体   繁体   中英

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. 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 function:-

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=...

Kindly help me to solve this.

The exception comes from this part [A-Za-z\\d#$@!%&*?] that characters after \\d makeing exceptions. just change it to 0-9 if you want to match a digit; something like : [A-Za-z0-9#$@!%&*?] or moving it to end like [A-Za-z#$@!%&*?\\d] .
But your complex regex will result after a long time ;).

Note: There are some rare results of using \\d inside [..] ;).

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM