Forum Moderators: coopster

Message Too Old, No Replies

Explain preg match

         

boxfan

6:04 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



Hello,

Can someone help me to understand what is going on here with this preg_match?

if(preg_match("`^!songlike ([A-Za-z0-9\'\, ]+)$`", $message, $m)) {

I understand that we're asking if $message equals!songlike and followed by any character A-Z, a-z, and 0-9 with no spaces between them and assigns it to $m.

I know the ^ marks the beginning of the match and $ is the end of the match. But the rest I can't figure out.

Can someone give me a item by item explanation?

Thanks!

mcibor

8:35 pm on Nov 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi BoxFan!

found on
[regular-expressions.info...]

`^!songlike ([A-Za-z0-9\'\, ]+)$` :

` - start of regex, you may use / as well (am not sure what is it really for)
^ - must begin with
! - I'm not sure now, it may be - lookup not matching, but then the syntax is slightly different
songlike - contains word songlike
- followed by space
( - start another group
[ - start character class or group
[A-Za-z0-9\'\, ] - match any letter, number, quote, comma and space
+ - must match at least once (therefore "songlike " would not match, but "songlike '" would
) - end of group
$ - ending must also match
` - close regex

Hope this helps

You can also try
[en.wikipedia.org...]

Regards
Michal

boxfan

5:25 am on Nov 24, 2006 (gmt 0)

10+ Year Member



Thank you very much!

I'm a little confused by what you said here

+ - must match at least once (therefore "songlike " would not match, but "songlike '" would.

Why would a space not be a match with

[A-Za-z0-9\'\, ]

Isn't a space a matching character just like a comma, letter, or number?

eelixduppy

5:41 am on Nov 24, 2006 (gmt 0)



The only reason that it doesn't match is because you have a space between "songlike" and the rest. If you close the space then it will match both examples given.

$pattern = "`^songlike([A-Za-z0-9\'\, ]+)$`";

Since there was only one space after the text in the example, that matches the space between songlike and the rest, but you still need to have at least one of the characters specified (because of the '+') to come AFTER that space in order to find the match.

If there are two spaces after the text, then it will come up with a match the way you have your pattern now:


$text = "songlike ";

boxfan

1:37 pm on Nov 24, 2006 (gmt 0)

10+ Year Member



I see, thank you!

mcibor

7:52 pm on Nov 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Eelix,
what was the "!" for?

Michal

PS. I tried the two spaces, but forgot, that browser shows only one ;) therefore I used the apostrophe

eelixduppy

10:42 pm on Nov 26, 2006 (gmt 0)



>>what was the "!" for?

As far as Pattern Syntax [us2.php.net] is concerned the exclamation point is used for assertions. I'm not too familiar with advanced assertions but I don't think that the pattern above followed any of the rules, so I'm guessing it's just to be taken as the string literal.