I am tired of being frightened of regular expressions. The topic of this post is limited to PHP implementation of regular expressions, however, any generic regular expression advice would obviously be appreciated (i.e. don't confuse me with scope that is not applicable to PHP).
The following (I believe) will remove any whitespace between numbers. Maybe there is a better way to do so, but I still want to understand what is going on.
$pat="/\b(\d+)\s+(?=\d+\b)/";
$sub="123 345";
$string=preg_replace($pat, "$1", $sub);
Going through the pattern, my interpretation is:
\b
A word boundary\d+
A subpattern of 1 or more digits\s+
One or more whitespaces(?=\d+\b)
Lookahead assertion of one or more digit followed by a word boundary?- Putting it all together, search for any word boundary followed by one or more digits and then some whitespace, and then do some sort of lookahead assertion on it, and save the results in $1 so it can replace the pattern?
Questions:
- Is my above interpretation correct?
- What is that lookahead assertion all about?
- What is the purpose of the leading
/
and trailing/
?