Forum Moderators: coopster

Message Too Old, No Replies

Universally Escaping the Delimiters in a Regexp

         

geckofuel

2:21 pm on Mar 17, 2003 (gmt 0)

10+ Year Member



In the following expression, the delimiter is "{" Notice that in parenthesis I am being completely inclusive (all characters). I would like to be able to tell the system to escape all delimiters within the (.*) portion of the expression. Is this possible?

preg_match("{<\/head>(.*)powered}i", $webpage_contents, $matches2);

DrDoc

2:49 pm on Mar 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, just do a replace, like this:

$title = preg_match("{<\/head>(.*)powered}i", $webpage_contents, $matches2);
$title = preg_replace("[^A-Za-z0-9]","\\$1"$title);

($1 will be the match, right?)

geckofuel

3:16 pm on Mar 17, 2003 (gmt 0)

10+ Year Member



Dr. Doc,
I'm not sure that that will work. The problem is that the first preg_match call isn't retrieving the substring that I'd like it to retrieve.

I'd like to retreive everything between:

</head> ...... powered

Unfortunately, I've not been able to find a delimiter that won't be contained in the " ...... " part. Now, I'd like to be able to run this preg_match so that all delimiters in the ..... part of the expression get escaped. Are you saying that I should just go ahead and escape all characters in the string before doing the match?

andreasfriedrich

3:28 pm on Mar 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I [webmasterworld.com] wrote at 11:00 AM on Mar. 17, 2003 in message #10 [webmasterworld.com]
The first sign of the pattern string is considered the pattern delimiter. In this case it is the single quote. Not using the slash when matching html tags has the advantage that you do not have to escape the slash in your pattern.

Notice emphasis on pattern. There is no need to escape the RE delimiter in the string. All you need to make sure is that it is not used or escaped in your pattern.


$string = 'Aaron rules!';
preg_match("!^(\w+)!", $string, $m);

will work just fine and match Aaron. If you wanted to match rules! you would either use a different delimiter thatīs not contained in your pattern or escape the exclamation mark.


preg_match("{(\w+!)}", $string, $m);
preg_match("!(\w+\!)!", $string, $m);

I hope that sheds some light on my last statement.

Andreas

andreasfriedrich

3:46 pm on Mar 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have a string that you want to use as a pattern and want to escape any characters in that string that would have special meaning in RE context use the &preg_escape& function. This is probably not what you want to do. You would not use &preg_escape& in the string but on the pattern.

Andreas