regex drive me crazy
| From: | jonas_weber @ gmx . ch | Date: | Wed, 01 Oct 2003 20:02:19 +0000 |
| Subject: | regex drive me crazy | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-164616@lists.php.net to get a copy of this message | ||
1. The PHP manual sais to escape the escape char, it has to be written twice*, but:
$term = preg_replace('/(\\)/', 'backslash $1', $term);
causes an error** while using three backslashes (see 2.) works.
2.1.
$term = "beg \ end";
print preg_replace('/(\\\)/', 'backslash $1', $term);
returns: beg backslash \ end
2.2.
$term = "beg \\ end";
print preg_replace('/(\\\)/', 'backslash $1', $term);
returns: beg backslash \ end (the same as 2.1.)
2.3.
$term = "beg \\\ end";
print preg_replace('/(\\\)/', 'backslash $1', $term);
returns: beg backslash \backslash \ end
Does this make ANY sense?
- jns
* http://www.php.net/manual/en/pcre.pattern.syntax.php
** "Warning: Compilation failed: missing ) at offset 3 in /home/jonas/public_html/test01.php on line 3"
Am Mittwoch, 01.10.03 um 19:14 Uhr schrieb CPT John W. Holmes:
Regular expressions are for "pattern matching"... not matching everything BUT a pattern.good point! ;)