Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Positive lookahead? #50

Open
santi77 opened this issue Dec 3, 2018 · 4 comments
Open

Positive lookahead? #50

santi77 opened this issue Dec 3, 2018 · 4 comments

Comments

@santi77
Copy link

santi77 commented Dec 3, 2018

I'm trying to get a random password with some restrictions, this is my regex

^(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[@$._*#&%]).{8,16}$

This regex says:

  • have one or more digits
  • have lowercase letters
  • have uppercase letters
  • have one or more that symbols
  • lenght between 8 and 16 chars

When I try to run this on Xeger the code hangs, It is possible to do this?

@StefH
Copy link

StefH commented Dec 4, 2018

@santi77 - I think your pattern should be like:

string pattern = "^(?.\\d)(?.[a-z])(?.[A-Z])(?.[@$\\._\\*#&%]).{8,16}$";
string value = new Xeger(pattern).Generate();

@santi77
Copy link
Author

santi77 commented Dec 4, 2018

That works, thank you!!!

@StefH
Copy link

StefH commented Dec 5, 2018

I've my doubts if this actually works.
lookahead is not implemented

Also when using Rex (https://rise4fun.com/rex), I get this error:
The following constructs are currently not supported: anchors \G, \b, \B, named groups, lookahead, lookbehind, as-few-times-as-possible quantifiers, backreferences, conditional alternation, substitution

So in your case, to generate a valid pattern, you can use like this:

"\\d{2,4}[a-z]{2,4}[A-Z]{2,4}[0-9a-zA-Z]{2,4}"

However this is not really random...

@gregsdennis
Copy link

gregsdennis commented Jan 20, 2022

This worked for me as well, but I don't understand why. The construct (?=.<something>) works for online regex validators, but (?.<something>) is reported as invalid yet it works in .Net. And it doesn't work for this online .net validator: http://regexstorm.net/tester

Looks like it's an issue with this lib. The first syntax works for validation (Regex) but not for generation (Xeger).

Note also that (?.(dog)) only ever creates string that start with a random char, then "dog", then more randomness. I'll never get "dog" in another position within the generated string, which is what (?=.*(dog)) allows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants