gprobst Posted January 10, 2010 Share Posted January 10, 2010 I've been going through some simple regex tutorials on the web, and while most of what I've come across makes sense, I can't wrap my head around this password validation example... preg_match("/^.*(?=.*{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", "AFdfj7aso") I'm fairly certain that I'm not understanding the ^.* and the .*$ elements of it properly. My understanding of ^.* is this... match any alphanumeric character zero or more times at the beginning of the string. My understanding of .*$ is this... match any alphanumeric character zero or more times at the end of the string. I think the zero or more part is what is confusing me. If we're looking for an alphanumeric character zero or more times, what is the point? If a non-alphanumeric character is at the beginning or end, then we have occurrences of an alphanumeric character zero times, so it doesn't seem like this is testing for the presence of an alphanumeric at the beginning or the end. If someone could please enlighten me to my ignorance of this, I'd really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/187980-i-dont-understand-and/ Share on other sites More sharing options...
cags Posted January 10, 2010 Share Posted January 10, 2010 That seems like a bit of a confusing pattern to me. The caret (^) matches at the start of the string and the asterix (*) matches 0 or more as you said, but the fullstop (.) doesn't match just alphanumeric characters it will match all (with rare exceptions like \n by default) characters. Where did you find that pattern and what format is it supposed to validate exactly, because I can't imagine it does what you want it to (well it won't actually run as-is, so it can't). Quote Link to comment https://forums.phpfreaks.com/topic/187980-i-dont-understand-and/#findComment-992446 Share on other sites More sharing options...
nrg_alpha Posted January 10, 2010 Share Posted January 10, 2010 Further issues involve something like .*{8,} What this is saying is, match anything (other than a newline), zero or more times, 8 times minimum ({8,} is called an interval, and when there is comma followed by nothing, this denotes that the maximum limit is limitless). So as you can see, matching something zero or more times, 8 times minimum doesn't make sense (afterall, we are talking about matching something consecutively). Here are some links that can help you get a better grasp of regex: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax http://www.regular-expressions.info/ http://weblogtoolscollection.com/regex/regex.php Going through these links and really taking your time to read, participate and play along will go a long way in learning regex more indepth. Quote Link to comment https://forums.phpfreaks.com/topic/187980-i-dont-understand-and/#findComment-992465 Share on other sites More sharing options...
gprobst Posted January 11, 2010 Author Share Posted January 11, 2010 The code snippet was from http://www.webcheatsheet.com/php/regular_expressions.php In the snippet the .*{8,} was originally .{8,}, but in their description they refer to it as .*{8,} (this was not the first inconsistency I ran into on their examples. The format that it was suppose to validate was a password with at least one capital letter, one lowercase letter, one number, with a string length of at least 8 characters. I guess the multiple inconsistencies between the descriptions and code snippets should have been a clue to me to find some better examples, but I'm a bit slow sometimes. I'll check out the links you suggestions. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/187980-i-dont-understand-and/#findComment-992502 Share on other sites More sharing options...
nrg_alpha Posted January 11, 2010 Share Posted January 11, 2010 In the snippet the .*{8,} was originally .{8,}, but in their description they refer to it as .*{8,} Ah.. well, .{8,} makes more sense... (match anything [other than a newline] 8 consecutive times minimum). So yeah, that's the thing with snippets.. if they are not crossed checked (from the author's stand point), problems / issues like this can arise. Unfortunately, the uninitiated get caught in this crossfire of confusion, making learning something as daunting as regex even more frustrating / intimidating. No worries.. there are other sources out there (like us) to help out Quote Link to comment https://forums.phpfreaks.com/topic/187980-i-dont-understand-and/#findComment-992507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.