Re: Match anything between two " that is not a " except if it is escaped...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



mathieu leddet writes:
Hi everyone,

I am struggling with regular expression trying to match strings
delimited by double quotes, but taking into consideration that \" is not
a string ending character.

......
// pattern for catching strings between "
$pattern = '#"([^"]*)"#';

.....
$out contains : this is a string : <b>"Hi everyone my name is \"</b>Mathieu\<b>"!"</b>
Here is a second string : <b>"PHP is just perfect"</b>

.....
--
Mathieu

If I right understand you scope. (yes, my English is bad)
You hope to get result such as:

this is a string : <b>"Hi everyone my name is \"Mathieu\"!"</b> Here is
a second string : <b>"PHP is just perfect"</b>

I try to fix you regular expression.
$pattern = '#"(.*?)(?<=[^\\\\])"#is';

attend to this: (?<=[^\\\\])

when PHP compile this string - inside it looks like (?<=[^\\])
when regular expression compile inside pcre library it looks like
 #"(.*?)(?<=[^\])"#is
this part: (?<=[^\])" is mean folow:
double quote, which not have leading backslash.


see folow:

max@maximus:~$ cat preg.php
<?php
$in = 'this is a string : "Hi everyone my name is \"Mathieu\"!" Here is
a second string : "PHP is just perfect"';


// pattern for catching strings between "
$pattern = '#"(.*?)(?<=[^\\\\])"#is';


// surround matching string with HTML span code to highlight
$replacement = '<b>"${1}"</b>';

// perform the reg exp replacement
$out = preg_replace($pattern, $replacement, $in);
echo $out,"\n\n";
max@maximus:~$ php preg.php
this is a string : <b>"Hi everyone my name is \"Mathieu\"!"</b> Here is
a second string : <b>"PHP is just perfect"</b>


Is this rigth?
--

Max Anotnov (idler at instanceof dot ru)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux