Re: regex drive me crazy

From: Date: Wed, 01 Oct 2003 21:23:38 +0000
Subject: Re: regex drive me crazy
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-164634@lists.php.net to get a copy of this message
* Thus wrote jonas_weber @ gmx. ch (jonas_weber@gmx.ch): > Am Mittwoch, 01.10.03 um 22:27 Uhr schrieb Chris W. Parker: > >Seeing as how you haven't had a response yet, I'll ask this: what > >exactly is your question? I'm not sure as to what it is that you want. > > My question are: > a) Why does the PHP manual say that backslashes get escaped by writing > them twice while this causes an error in my example (see below)? '\\' always gets translated to '\' So the string: '/(\\)/' translates to '/(\)/' which is an invalid regex, you'll get a complaint about unclosed paren. > b) Why does a different input (2.1. and 2.2.) result in the same output > (in the example I enclosed)? try: echo "beg \\ end\n"; echo "beg \ end\n"; Answer is also mentioned above. > c) Why do I hate regular expressions so much? perhaps because you're using them wrong. > > Choose one. I've answered all 5 of em so far. > >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 > > > >* 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" Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#164634) next »