Forum Moderators: coopster

Message Too Old, No Replies

Regex

I'm still lame in it

         

mcibor

10:27 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a string (from form) that looks like:

"Bartender wine for 3"
or
"Bartender beer for all!"

I have the allowed drinks stored in array
$menu = array("wine", "beer", "mustak");

and 3 is an id of the user - SELECT user FROM table WHERE id=3

The text returned should be:
"Here you are Sam, wine from Michal"
Michal is my name - easy to get
or
"Attention! Michal gives everybody beer"

My question is:
How can I accomplish that?

Moreover, if it's not a problem I would like to check if the text starts from "Bartender". If not, then just leave it (However that's easy with strpos)

Best regards
Michal Cibor

mcibor

9:54 am on Oct 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would be enough to get the text between "Bartender" and "for", and id or all! from the end

I tried
if(ereg("Bartender ([a-z]) for ([a-z0-9\!])", $text, $regs)) echo "Match found";
else echo "No match";

but it doesn't recognize the following text:
"Bartender wine for 3"

Where did I make a mistake?
Michal Cibor

coopster

3:54 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to match for one or more letters after the word "Bartender", notice the plus sign. Also, I would go with an "or", as in the word "all!" OR (notice the pipe symbol) a number. Here is a snippet to play around with:
$string = "Bartender wine for 3"; 
preg_match("/Bartender ([a-z]+) for (all\!¦[0-9]+)/Uis", $string, $names);
print '<pre>'; print_r($names); print '</pre>';
$string = "Bartender beer for all!";
preg_match("/Bartender ([a-z]+) for (all\!¦[0-9]+)/Uis", $string, $names);
print '<pre>'; print_r($names); print '</pre>';

The forum breaks the pipe symbol, you will have to rekey it if you copy and paste this code.

mcibor

7:30 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had to change the match to

/Bartender ([a-z]+) for (all!¦[0-9]+)/

It wasn't processing the all! and numbers greater than 9 (in your first version the number was cut to first digit.
However now seems to work just fine!

Thanks coop!
Michal Cibor

coopster

9:32 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, it should capture the "all!" just fine, but I accidentally left the "U" (Ungreedy) modifier on there, take it off and you should be OK.
preg_match("/Bartender ([a-z]+) for (all\!¦[0-9]+)/is", $string, $names); 

Don't forget that if you copy and paste, that pipe symbol needs to be rekeyed!

mcibor

5:11 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK coop!

Now works fine! But can you tell me the difference between:

/Bartender ([a-z]+) for (all\!¦[0-9]+)/is
and
/Bartender ([a-z]+) for (all!¦[0-9]+)/
?

Both work the same, as I see.

Regards
Michal

Thanks for everything!

coopster

3:30 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure, they will work the same. I escape the exclamation mark as it sometimes has a special meaning in regular expressions (negative assertions), but I always escape special chars in my regexs ... old habit I guess.

The two pattern modifiers [php.net] are also just carryovers. the "i" allows the pattern to match both upper and lower case letters and the "s" is just if you were using a dot metacharacter over a multiple line expression as it will also match newlines. Again, they are so common in most regexs I write that I simply forgot to pull them off in the example I posted.