Link to home
Start Free TrialLog in
Avatar of mwheeler_fsd
mwheeler_fsd

asked on

Complex regex with preg_match_all()

Dear Experts,

I've been handed a request to enforce a criteria for a password policy.

I will the be the first to say regular expressions are not my strong suit. So, while this may not be as complex as it seems, it most certainly is to me.

The request is:

two of a capital, special character, or number.

I understand the individual checks:

preg_match_all("/[A-Z]/",$subject,$matches);
preg_match_all("/[\W]/",$subject,$matches);
preg_match_all("/[0-9]/",$subject,$matches);

It's how to combine them and create the AND/OR that slips past me.

I considered doing it by keeping track of the individual results and doing a comparison afterward, but it occurred to me there must be a way to do it in one expression.

So, my question is - is there a way to do this in one expression and, if so, how?

Alternately, would it be cleaner to check the $subject individually and compare the results afterwards?

I am trying desperately not to break the rules by asking more than one question with this question, but finding it extremely difficult. The reason is if an Expert with regular expressions says "do it individually" then there is no reason to pursue the other option. If the best approach is to combine the checks into one expression, of course this is the route I would like to pursue, and learn from.

Thank you in advance.

Best,
Mike
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwheeler_fsd
mwheeler_fsd

ASKER

Yes, your assumption on the logic was correct. I had the opportunity to go through the spec on a conference call where that had been cleared up. When I posted the question, I cut and pasted it from the spec doc. Apologies for my assumptive oversight.

In any event, thank you for your excellent solution!

Best,
Mike
I would also like to thank kaufmed for your insight, and will definitely check out your article.

Best,
Mike
... it occurred to me there must be a way to do it in one expression ...
But why? Why not write simple, easy-to-understand code?

But with that said, please bookmark a link to the REGEX cheat sheet, where Dave Child has some helpful information for those of us, myself included, who are thickheaded about regular expressions.
http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/png/

HTH, ~Ray
Dear TerryAtOpus and Kaufmed,

Please accept my humble explanation for a redistribution of my earlier awarded points.

I have been a member of EE for 8 years, and a programmer for 25 years. While I strive to follow the protocol, I was a bit hasty, and most certainly naive, about how to award points for your excellent answers. So much so, I consulted one of the outstanding mediators (who I will leave nameless, but greatly appreciated) as to what the proper protocol would be in this scenario, and my dilemma.

He offered up a well balanced explanation of the different ways these situations could be approached and, after more consideration, I feel I must award kaufmed a portion of the points.

The last thing I want to do is discourage or upset any of the Experts that work so hard to provide solutions, so please accept my apology for this. Here is my reasoning;

TerryAtOpus - your answer was spot on, and even having to take some assumptions from my lack of clarity, you solved the issue at hand. However, kaufmed's additional link to his excellent article was very helpful for me in understanding why/how this works.

I realize that the goal is to answer the question, but the additional foundation information from kaufmed helps me to improve as a programmer. I clearly understand that was not what I asked for, and I am hoping, with the deepest respect, you can understand my reasoning.

I take this site very seriously and hope that both of you will understand why, and how, I arrived at this conclusion.

Your time and dedication is greatly appreciated.

With best regards,
Mike

PS - Again through my own hard lesson learned, I must thank Ray_Paseur for his response as well. I apologize for this, and most certainly have a better understanding of the protocol and workings in this scenario. My naivete in this situation is quite humbling.
Unexpected, but appreciated  = )
You are quite welcome and I hope my explanation is well received. Your article is excellent.

Best regards,
Mike
Thanks, Mike, for caring about the people at the other end! It's appreciated!
Thank you SO much for understanding. A learning experience for me, and am truly appreciative of the gracious understanding.

Best wishes to all,
Mike