Regex that should be simple

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

Moderator: General Moderators

Post Reply
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Regex that should be simple

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regex that should be simple

Post by AbraCadaver »

Works great for me, but echo $matches[1].
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Re: Regex that should be simple

Post 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!!!
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Re: Regex that should be simple

Post by xterra »

LOL. I'm such an idiot. Got it now, thanks!
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Re: Regex that should be simple

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regex that should be simple

Post 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]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Re: Regex that should be simple

Post 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:/
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regex that should be simple

Post by AbraCadaver »

But you haven't added the s modifier in the most recent code you posted.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Re: Regex that should be simple

Post 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.
Post Reply