Page 1 of 1
Regex that should be simple
Posted: Wed Jun 09, 2010 12:51 pm
by xterra
This RegEx works on those online testers, but in PHP, it says no match found! Here it is:
Code: Select all
if (preg_match("/Store Hours (.*?) Store Phone/", $test, $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
The text looks like this:
Store Hours Mon-Fri 8:00AM-10PM Sat/Sun 10AM-10PM Store Phone XXX-XXX-XXX
I'm just trying to get "Mon-Fri 8:00AM-10PM Sat/Sun 10AM-10PM".
This should be so simple! But in PHP it doesn't work, any ideas?
Thank you!
Re: Regex that should be simple
Posted: Wed Jun 09, 2010 12:56 pm
by AbraCadaver
Works great for me, but echo $matches[1].
Re: Regex that should be simple
Posted: Wed Jun 09, 2010 12:58 pm
by xterra
This is so weird because the condition isn't even returning true so the echo statement isn't getting called. I'm using PHP5 which is probably the reason. I even tried hardcoding the text in like this:
if (preg_match("/Pharmacy Hours (.*?) Site to Store/", "Store Hours Mon-Fri 8:00AM-10PM Sat/Sun 10AM-10PM Store Phone XXX-XXX-XXX", $matches)) {
echo "Match was found <br />";
echo $matches[1];
echo "Am I in? Did I get called?";
}
Absolutely nothing! Ugh!!!
Re: Regex that should be simple
Posted: Wed Jun 09, 2010 1:17 pm
by xterra
LOL. I'm such an idiot. Got it now, thanks!
Re: Regex that should be simple
Posted: Wed Jun 09, 2010 1:31 pm
by xterra
What's interesting is it seems to work when it's just 1 line , like above. But when it's in a large paragraph it doesn't. Is there a special dilimeter I am missing?
Re: Regex that should be simple
Posted: Wed Jun 09, 2010 1:39 pm
by AbraCadaver
xterra wrote:What's interesting is it seems to work when it's just 1 line , like above. But when it's in a large paragraph it doesn't. Is there a special dilimeter I am missing?
The . in the pattern doesn't match a newline, so you need to use the s modifier:
[text]"/Store Hours (.*?) Store Phone/s"[/text]
Re: Regex that should be simple
Posted: Wed Jun 09, 2010 8:58 pm
by xterra
Hi,
I appreciate your patience, unfortunately it's still not working.
The oddest thing, it ONLY works when I don't use a variable. I.e., it works if I do:
Code: Select all
if (preg_match("/Pharmacy Location (.*?) End Pharmacy Location/","MANUALLY ENTER IN THE LONG BLOB HERE", $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
But will not work if I do:
Code: Select all
if (preg_match("/Pharmacy Location (.*?) End Pharmacy Location/","$blob", $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
Found the problem, but not the solution The reason for the above is because when I manually enter it in, I'm not including linebreaks. Thus, I'm still having the linebreak issue:/
Re: Regex that should be simple
Posted: Thu Jun 10, 2010 10:05 am
by AbraCadaver
But you haven't added the s modifier in the most recent code you posted.
Re: Regex that should be simple
Posted: Thu Jun 10, 2010 5:54 pm
by xterra
My fault, that was just in the post. It turns out the problem was I had an extra space in between "Store Hours" and the (*.*) thing. I didn't realize RegEx was so picky! Thanks Abra.