|
Posted by Toby A Inkster on 11/30/07 10:34
Charles wrote:
> preg_match('/http\:\/\/\[\\w.-\]+/','http://www.regular-
> expressions.info/javascriptexample.html',$matches);
Firstly, don't backlash-escape the square brackets:
preg_match('/http\:\/\/[\\w.-]+/','http://www.regular-expressions.info/
javascriptexample.html',$matches);
That should work. But there's still room for improvement -- to make it
more readable. You don't need to backslash escape the backslash itself,
nor the colon:
preg_match('/http:\/\/[\w.-]+/','http://www.regular-expressions.info/
javascriptexample.html',$matches);
Also, PHP (and Perl) allows you to choose a character other than slash as
a delimited (i.e. the character at the beginning and end of the
expression). In this case, lets choose a hash instead:
preg_match('#http:\/\/[\w.-]+#','http://www.regular-expressions.info/
javascriptexample.html',$matches);
Because we're not using a slash as a delimiter, it means we no longer need
to backslash-escape the slashes within the expression:
preg_match('#http://[\w.-]+#','http://www.regular-expressions.info/
javascriptexample.html',$matches);
That's much more readable, right?
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 5 days, 17:19.]
Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/11/28/itunes-sharing/
Navigation:
[Reply to this message]
|