tags:

views:

17

answers:

2

I am new to regexp in php. I am just trying to do a simple preg_match on two strings. Here is my code:

$pattern = '\\w*'.strtolower($CK);

if(preg_match($pattern, $rowName, $matches) == true)
            {
                echo 'true';

            }

But I keep getting the error:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /home/daskon/public_html/reflexInvestor_dev/php/rss_functions.php on line 319

I imagine it's because I need to put something after the pattern, but I can;t seme to find what that is. When I try the same pattern in a regexp tester, it works fine.

+2  A: 

You need to wrap the pattern in a delimiter that marks the beginning and end. Any "non-alphanumeric, non-backslash, non-whitespace" character works. / is very common, so:

$pattern = '/\\w*' . strtolower($CK) . '/';

The reason for the delimiter is you can include pattern modifiers in the pattern, but they appear after the delimiter

Michael Mrozek
+1  A: 

You need to wrap the pattern in delimiting characters (I like to use # since I rarely need them inside of the regex):

if (preg_match('#' . $pattern . '#', $rowName, $matches)) {

(You also don't need the == true, it's redundant since that's what the if does anyway internally...)

ircmaxell
Interesting; I use `#` too, and generally people ask me why I'm the only person in the world to use it
Michael Mrozek
I don't have anything against other delimiters, I just like `#`... I don't use any of the regex meta-characters, so `^`, `$`, etc are out. And as for `/`, I just find that I need that character more often than I need `#` (inside a regex at least). So I like using `#` (and make it a habit)...
ircmaxell
Thanks!So how come if I am matching the keyword 'mcdonald' to 'mcdonalds breakfast' using this regular expression I get no results? I get other results that match, but not that one.
pfunc
Are you sure that's the exact contents of those strings? No case changes? And I'm assuming that you're talking about `$CK` has `mcdonald`?
ircmaxell
I'm sorry, I am getting results. Was echoing out the wrong thing :p.Thank you.
pfunc