Connecting Tech Pros Worldwide Help | Site Map

Want regular expression to stop at first occurrence of word

 
LinkBack Thread Tools Search this Thread
  #1  
Old February 26th, 2007, 05:25 AM
e_matthes@hotmail.com
Guest
 
Posts: n/a
Default Want regular expression to stop at first occurrence of word

Hello all,

Say I have the following string:

$string = "list of whales: white beluga whale humpback whale atlantic
humpback whale";

I want to pull out the first kind of whale (white beluga). I want
this to work regardless of whether the first whale is a white beluga,
or a humpback, etc. I tried:

$regExp = "/(whales:)(.*)(whale)/";
$outputArray = array();
if ( preg_match($regExp, $string, $outputArray) ) {
print "$outputArray[2]<br>";
}

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.


  #2  
Old February 26th, 2007, 06:35 AM
Heiko Richler
Guest
 
Posts: n/a
Default Re: Want regular expression to stop at first occurrence of word

e_matthes@hotmail.com wrote:
Quote:
$string = "list of whales: white beluga whale humpback whale atlantic
humpback whale";
Quote:
$regExp = "/(whales:)(.*)(whale)/";
if ( preg_match($regExp, $string, $outputArray) ) {
Quote:
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...dy_expressions

Heiko
--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage
  #3  
Old February 26th, 2007, 05:05 PM
e_matthes@hotmail.com
Guest
 
Posts: n/a
Default Re: Want regular expression to stop at first occurrence of word

On Feb 25, 10:25 pm, Heiko Richler <heiko-rich...@nefkom.netwrote:
Quote:
e_matt...@hotmail.com wrote:
Quote:
$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...dy_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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.