Lodder
Lodder

Reputation: 19733

What do these certain symbols/parts mean in preg_match?

I know a little about preg_match, however there are some that look rather complex and some that contain symbols that I don't entirely understand. For example:

On the first one - I can only assume this has something to do with an e-mail address and url, but what do things like [^/] and the ? mean?

preg_match('@^(?:http://)?([^/]+)@i', $variable);

.....

In the second one - what do things like the ^, {5} and $ mean?

preg_match("/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/", $variable);

It's just these small things I'm not entirely sure on and a brief explanation would be much appreciated.

Upvotes: 0

Views: 728

Answers (4)

E. Lüders
E. Lüders

Reputation: 1495

first one: [^/] = everything but no slash

second one: ^ look from beginning of $variable {5} exactly 5 occurencies of [A-Z] $ look until end of $variable reached

combination of ^ and $ means that everything between that has to apply to $variable

Upvotes: 1

Femaref
Femaref

Reputation: 61467

First one:

The @ actually don't have anything to do with the content that is matched. Usually, you use / as the delimiter character in a regex. Downside is, that you need to escape it everytime you want to use it. So here, @ is used as the delimiter.

[^/] is a character group. [/] would match only the / character, ^ inverts this. [^/] matches all characters except the /.

Second one:

^ matches the beginning of the string, $ the end of the string. You can use this to enforce that the regex has to apply to the whole string you are matching on.

{5} is a quantifier. It is equivalent to {5,5} which is minimum 5, maximum 5, so it matches exactly 5 characters.

Upvotes: 1

Wearybands
Wearybands

Reputation: 2455

Here are the direct answers. I kept them short because they won't make sense without an understanding of regex. That understanding is best gained at http://www.regular-expressions.info/tools.html. I advise you to also try out the regex helper tools listed there, they allow you to experiment - see live capturing/matching as you edit the pattern, very helpful.

Simple parentheses ( ) around something makes it a group. Here you have (?=) which is an assertion, specifically a positive look ahead assertion. All it does is check whether what's inside actually exists forward from the current cursor position in the haystack. Still with me? Example: foo(?=bar) matches foo only if followed by bar. bar is never matched, only foo is returned.

With this in mind, let's dissect your regex:

 /^.*(?=.{4,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/

Reads as:
    ^.* From Start, capture 0-many of any character
(?=.{4,}) if there are at least 4 of anything following this
(?=.*[0-9]) if there is: 0-many of any, ending with an integer following
(?=.*[a-z]) if there is: 0-many of any, ending with a lowercase letter following
(?=.*[A-Z]) if there is: 0-many of any, ending with an uppercase letter following
    .*$ 0-many of anything preceding the End

Upvotes: 4

Gerald Versluis
Gerald Versluis

Reputation: 34063

Although I am not a fan of just posting links, I think a regex tutorial would be too much. So check out this Regular Expression cheat sheet it will probably get you on your way if you already have a little understanding of what it does.

Also check out this for some explanations and more helpful links; http://coding.smashingmagazine.com/2009/06/01/essential-guide-to-regular-expressions-tools-tutorials-and-resources/

Upvotes: 1

Related Questions