Want to pull the data between two words

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
smacky311
Forum Newbie
Posts: 2
Joined: Mon May 18, 2009 11:56 am

Want to pull the data between two words

Post by smacky311 »

I have the following pattern:

Code: Select all

 
 $regexp = "/Country: .* City: /";
        if (preg_match($regexp, "Country: United States (XX) City: (Unknown City?) ", $matches)) {
          echo "Match was found <br />";
          echo $matches[0];
        }
 
It returns "Country: United States (XX) City: ". I only want the regular expression to return: "United States (XX)". How can this be done?
Last edited by Benjamin on Fri May 22, 2009 12:01 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Want to pull the data between two words

Post by prometheuzz »

Try:

Code: Select all

'/(?<=country:).*?(?=city)/is'
Post Reply