On 14 March 2011 15:18, Martin Scotta <martinsco...@gmail.com> wrote:
> <?php
> function test($str) {
>    static $re = '/(^|[^\\\\])\'/';
>    static $change = '$1\\\'';
>
>    echo $str, PHP_EOL,
>        preg_replace($re, $change, $str), PHP_EOL, PHP_EOL;
> }
>
> test("str '' str"); // bug?
> test("str \\'\\' str"); // ok
> test("'str'"); // ok
> test("\'str\'"); // ok
>

Your regex is ...



(^|[^\\\\])'

Options: case insensitive; ^ and $ match at line breaks

Match the regular expression below and capture its match into
backreference number 1 «(^|[^\\\\])»
   Match either the regular expression below (attempting the next
alternative only if this one fails) «^»
      Assert position at the beginning of a line (at beginning of the
string or after a line break character) «^»
   Or match regular expression number 2 below (the entire group fails
if this one fails to match) «[^\\\\]»
      Match a single character NOT present in the list below «[^\\\\]»
         A \ character «\\»
         A \ character «\\»
Match the character “'” literally «'»



I think [^\\\\] is wrong and you want it to be ...

(?!\\\\)

or

(?!\\{2})


With that, the output is ...

str '' str
str \'\' str

str \'\' str
str \\'\\' str

'str'
\'str\'

\'str\'
\\'str\\'



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to