Forum Moderators: coopster
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!
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
$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 ";
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.