Page 1 of 1

backlash in regex and php?...

Posted: Mon Dec 20, 2004 12:10 am
by sebnewyork
Hi
I am trying to use a regular expression, but it seems that php doesn't like the backlashes in the expression, because it returns:
"Delimiter must not be alphanumeric or backslash in...bla bla bla".

the regular expression appreas in the code:

Code: Select all

<?php
$pattern ="include\('^.*?$'\)";
?>
include\('^.*?$'\)

beeing the expression (i am new to regex, so I wouldn't be surprised if it doesn't make any sense).
Just so you know, with this expression I'm trying to match anything between
"include('"
and
"')"

However, I should first be able to use backlashes as needed in my regex. How do I do that without upseting php parsing?

Posted: Wed Dec 22, 2004 2:12 pm
by skehoe
Hey,

You don't need the backslashes... Here's an example:

Code: Select all

<?php
$string = 'include("/foo/bar/bif/baz.php")';
$pattern = '/include(.*)/';

if (preg_match($pattern, $string, $matches))
{
    print_r($matches);
}
else
{
    echo "Match Failed\n\n";
}

?>
~Scott

Posted: Wed Dec 22, 2004 2:48 pm
by rehfeld
sebnewyork-
wheres your delimiters?

you dont have any. you MUST use delimiters w/ pcre

do like this

Code: Select all

$pattern ="/include\('^.*?$'\)/";

btw- that regex pattern isnt going to work. it needs to be something like this

Code: Select all

$pattern ="/include\s*\('([^']+)'\)/";