Reply to Re: Hard time with a regex...

Your name:

Reply:


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/

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация