Serpent_Guard wrote:Once again, your solution works perfectly. Thank you.
You're welcome.
Serpent_Guard wrote:I tried googling to find out what '(.+?)' really does, and if I could find anything else to use that'd do the job, like '(\S+)', but I couldn't find anything. Do you have a link to such a site or document?
The '.' (DOT) will match any characters except new line characters (but in your case it also matches new line characters because you added the s-flag at the end).
A '+' means: "one or more". So, the regex:
matches one or more characters of any type except new line characters.
Now, when you add a question mark after it, the DOT-PLUS will be made reluctant opposed to being greedy. What this means can best be explained by an example:
Code: Select all
$text = 'aaaBaaaBaaaBaaa';
if(preg_match('/.+B/', $text, $match)) {
echo "Greedy match : $match[0]\n";
}
if(preg_match('/.+?B/', $text, $match)) {
echo "Reluctant match : $match[0]\n";
}
when running that snippet, I'm sure the difference is clear (but, if not, just post back!).
And about
\S. This is called a "negated shorthand character classes". What the $%#@ is that I hear you thinking... Okay, you probably know the character class
[0-9] which matches one of the numerical digits 0, 1, 2, 3, ... , 9. Now, to since that character class is used so often, there's a shorthand for it, which is:
\d. This
\d is called a "shorthand character classes".
Now the opposite of
\d is
\D and matches any character
except a numerical digit (
\D is called the "negated shorthand character classes" of
\d).
So, to wrap it up,
\S is the opposite of
\s which matches any white space character (new line, tab, space...). So
\S will match any character except a white space character.
Okay class, that's it for today!
; )
If you're serious in learning some regex, the best on-line resource to learn them is this site:
http://www.regular-expressions.info/
Good luck!