4

I am trying to use a regular expression and it's just breaking my brain. I'll think I have it and the the script just breaks. I need to try to give people a way to report their location via SMS. I ask them to text me a string that looks like this:

I give up, stopping for the night. Find me at Main Street and Pine, Atlanta, GA.

And I want to break it at Find me at. I need to capture everything to the right of that (essentially their street corner) and I need to be able to detect if they didn't use proper capitalization for Find.

Any suggestions?

3
  • 1
    Doesn't really answer your question, but I find this site useful: http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/. Download the PDF and keep it handy. Commented May 17, 2011 at 1:23
  • @Tieson Good resource, though their email regex is horrendous. Commented May 17, 2011 at 1:33
  • @Crashspeeder No doubt. I think Dave even admits as much somewhere on the site. :) Commented May 17, 2011 at 1:35

5 Answers 5

3

preg_match("/[Ff]ind me at (.+)/", "I give up, find me at street 1 and street 2") will capture everything after the "find me at"

Sign up to request clarification or add additional context in comments.

Comments

1

The following regex will work for you:

Find me at (.*)$

If you want it to ignore the case, you can specify it using the IgnoreCase option (?i:)

1 Comment

@Crashspeeder You're correct. For some reason I thought that was the begining of the text :)
1

Give this a shot: /find me at (.*)$/i

2 Comments

It looks like this one would work, but I saw the last entry first. Would this regex also catch a different case with find/Find?
@mcleodm3 Yes, that's what the i after the delimiter means. The whole regular expression is case insensitive.
1

Well, a non-regexp solution comes to mind.

$string = 'I give up, stopping for the night. Find me at Main Street and Pine, Atlanta, GA.';
$findMe = 'Find me at';
$address = substr(strstr($string, $findMe), strlen($findMe));
if ($address == '') {
    // no match
}

Then there is a regexp-based solution.

preg_match('/Find me at (.*)$/', $string, $match);
if (!isset($match[1])) {
    // no match found
} else {
    $address = $match[1];
}

Comments

0

I would just preg_split(). If you know what you want should always be at the end.

$str = "I give up, stopping for the night. Find me at Main Street and Pine, Atlanta, GA."

$strSplit = preg_split("/Find me at /i", $str);

$whatYouWant = $strSplit[1]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.