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