473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

Feb 26 '07 #1
2 72757
e_*******@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...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
Feb 26 '07 #2
On Feb 25, 10:25 pm, Heiko Richler <heiko-rich...@nefkom.netwrote:
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...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

Feb 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: amazononthemoon | last post by:
I have not been able to find any examples of the type of validation I need. I have numeric fields on a web page that I only want them to enter numbers with two decimal places. I would like to...
6
by: Joe Abou Jaoude | last post by:
hi, I need to verify that the user enter only one word in a textbox. So i thought to add a regularexpression validator and disallow the user to enter a space between the letters he wrote. how can...
2
by: malauddin | last post by:
Hi all, I am looking for a regular expression to find a word at the beginning of a line and then read the entire line. From the example below, I want to find the word "FN" and then read entire...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
6
by: rahatekarabhijeet | last post by:
I want regular expression which can extract the data between two symbols, e.g: <*skdjflsk (new line character) lksdjfklasjdfl (new line character) ksjdfkhasdkf (new line character) asdfjkhasdjk...
3
by: marco.minerva | last post by:
Hi! I have the following regular expression, that matches any word with number and letter: ]+-?/@\\.#&]*'? Now I must modify it so that also the word with a "_" inside are accepted. For...
1
by: st12iker | last post by:
Im looking for a regex expression that would enable me to match a pattern only before the first \n of a line. So for example if I had text if $\ = $$ and had text 123 DFG\n (1st line) $$ 345\n...
2
by: meena83 | last post by:
how to write a expression in Regular Expression Validator for -- a word that entered in a textbox must be ->first letter caps and all others small ->should not accept digits and special...
1
by: koduruabhinav | last post by:
Hi, some simple code required.i was trying but i was unable. But i want instances to get printed each time like we are printing comparing instance name. suppose if we take Each X_RAMB18E1 module...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.