Search code examples
c#.netregexfare

Not able to generate string from complex regular expression with Fare


I am using Fare library https://github.com/moodmosaic/Fare/ to generate a random string from regular expression. Up until now, it has been working properly.

What I wanted now is "The Password must have a minimum/maximum of 8 characters, including one special character, atleast 1 digit and atleast capital letter." Special characters allowed are !#$%^&*()=,.

for that, I have created the expression

 ^((?=.\d)(?=.[A-Z])(?=.*\W).{8,8})$

But it is not generating valid expression Please check what's the problem

I am generating the regular expression with:

var secret = new Xeger(ConfigurationManager.AppSettings["expression"]).Generate();
Console.WriteLine(secret);

I have updated the pattern requirement


Solution

  • If Fare doesn't support lookaheads, then you may instead try using a different regex expression devoid of any such lookaheads:

    ^([0-9])([A-Z])([!@#%*()$?+-])([a-zA-Z0-9]{5})$

    So this regex expression can be used for validating passwords but we would then be restricting user to input first a digit, second a capital letter, third a special character with rest being a mix of alphanumeric chars.