Jump to content

I don't understand ^.* and .*$


gprobst

Recommended Posts

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.