4

I would like to generate a string based on the regular expression I have.

The purpose of this function is when someone forgets their password, and completes the form for "Email New Password", I generate a random password that will be emailed to them. This new password must satisfy the conditions of my regex.

This is my regex:

'/.*^(?=.{6,})(?=.*[A-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$/'
3
  • And is the aim to do this with arbitrary regexes, or just this one specific regex? Commented Jan 9, 2013 at 1:57
  • 1
    There is a java library for this called Xeger. Although it us useless for PHP it might be a good starting point : code.google.com/p/xeger Commented Jan 9, 2013 at 1:59
  • What I tried is creating multiple random expression generators; where on generates random alphabets that aren't capitalized, another that is capitalized, another one that generates numbers (3 digits) and another that is generating characters from a list. Though, you can see that concatenating them and processing them is too heavy. I was wondering if a single operation based on regex can be implemented. I'll look into the Java plugin, but I prefer using PHP alone :) Thank you for sharing! Commented Jan 9, 2013 at 20:55

2 Answers 2

2

Did you take a look at the Hoa\Regex library? It generates strings based on regular expression with an isotropic random approach.

Sign up to request clarification or add additional context in comments.

4 Comments

This is a comment, not necessarily an answer.
So as an anwer, please check this article: mnt.io/P/… :-).
Yes, it's long overdue, but your research paper was quite a good read. Thank you for sharing this!
@IvanEnderlin did Hoa\Regex works for "[0-9A-Za-z_-]{10}[048AEIMQUYcgkosw]" ? This is the Regex for the VideoID of youtube. (here the reference webapps.stackexchange.com/questions/54443/…). The demo example form documentation works, but this one return "Undefined property: Hoa\Compiler\Exception\UnexpectedToken::$_tmpArguments". Maybe I shoud report on the github. Edit: the poster regex gives the same error. And yes, I'm aware that regex implement regex is a huge work.
1

Try this. No need for confusing regex:

function random_password( $length = 8 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr( str_shuffle( $chars ), 0, $length );
    return $password;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.