Optional Whitespace in Regular Expressions
When parsing HTML or other text formats, it's often necessary to ignore optional whitespace characters. To achieve this, you can modify your regular expression pattern using the following conventions:
For example, if you want to allow optional spaces between attribute names and values, modify your pattern to the following:
#<a href\s?="(.*?)" title\s?="(.*?)"><img alt\s?="(.*?)" src\s?="(.*?)"[\s*]width\s?="150"[\s*]height\s?="(.*?)"></a>#
This pattern will match HTML elements like these:
<code class="html"><a href="/wiki/File:Sky1.png" title="File:Sky1.png"><img alt="Sky1.png" src="http://media-mcw.cursecdn.com/thumb/5/56/Sky1.png/150px-Sky1.png" width="150" height="84"></a></code>
<code class="html"><a href="/wiki/File:TallGrass.gif" title="File:TallGrass.gif"><img alt="TallGrass.gif" src="http://media-mcw.cursecdn.com/3/34/TallGrass.gif" width="150"height="150"></a></code>
Note that the extra [space] characters in the original pattern are not included in this modified version.
The above is the detailed content of How to Ignore Optional Whitespace in Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!