Re: Re: Match anything between two " that is not a " except if it is escaped...
| From: | Jochem Maas | Date: | Thu, 17 Jan 2008 11:07:04 +0000 |
| Subject: | Re: Re: Match anything between two " that is not a " except if it is escaped... | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-267489@lists.php.net to get a copy of this message | ||
Max Antonov schreef:
mathieu leddet writes:better than our russian.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>.....-- MathieuIf 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: (?<=[^\\\\])attend? don't understand what you mean BUT you have given the OP the answer by changing his regexp to include a [negative] look behind assertion for the backslash. :-)
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)