You are here: Re: Want regular expression to stop at first occurrence of word « PHP Programming Language « IT news, forums, messages
Re: Want regular expression to stop at first occurrence of word

Posted by e_matthes on 02/26/07 17:56

On Feb 25, 10:25 pm, Heiko Richler <heiko-rich...@nefkom.net> wrote:
> e_matt...@hotmail.com wrote:
> > $string = "list of whales: white beluga whale humpback whale atlantic
> > humpback whale";
> > $regExp = "/(whales:)(.*)(whale)/";
> > if ( preg_match($regExp, $string, $outputArray) ) {
> > But, the output is "white beluga whale humpback whale atlantic
> > humpback", as I expected. I know how to stop after finding a single
> > character, but I can't figure out how to stop after finding a single
> > instance of a word. Any help? Thanks.
>
> RegEx are greedy. That means RegEx match as much as they can. This means
> your Expression stops with the last whale. To make it stop with the
> first whale make your Expression ungreedey:
>
> $regExp = "/(whales:)(.*?)(whale)/";
>
> or
>
> $regExp = "/(whales:)(.*)(whale)/U";
>
> see:http://en.wikipedia.org/wiki/Regular_expression#Greedy_expressions
>
> Heiko
> --http://portal.richler.de/Namensportal zu Richlerhttp://www.richler.de/ Heiko Richler: Computer - Know How!http://www.richler.info/ private Homepage

Thank you very much, both for the answer and the reference.

Eric

 

Navigation:

[Reply to this 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

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