[PHP] Re: Match anything between two " that is not a " except if it is escaped...
A good habit is to use the hex equivalent character for any character that has a special meaning in pregex expressions. e.g., space = \x20 "/" = \x2f "." = \x2e double quotes = \x3d etc. Then you won't have this type of problem and you won't have to use stuff like this: This is for double quotes: '/"[^"]*(?:.[^"]*)*"/' this is for single: '/\'[^\']*(?:.[^\']*)*\'/' It's almost impossible to debug this nonsense. Also, use delimiters that do not occur in your pattern. I almost never use "/"; and instead like "%". I assume you know the delimiters can be almost any character. mathieu leddet wrote: 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. ---8<--- - $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 = '#"([^"]*)"#'; // surround matching string with HTML span code to highlight $replacement = '"${1}"'; // perform the reg exp replacement $out = preg_replace($pattern, $replacement, $in); ---8<--- - $out contains : this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" This behaviour is normal considering my pattern (anything between two " that is not a "). But I don't know how to get this : this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" I would like my pattern to express : Anything between two " that is not a " except if it is escaped. Thanks for reading me, any help in return is welcome ! -- Mathieu -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Match anything between two " that is not a " except if it is escaped...
On Fri, 2008-01-18 at 12:28 +0200, Nikolay Ananiev wrote: > This is for double quotes: > '/"[^"]*(?:.[^"]*)*"/' > > this is for single: > '/\'[^\']*(?:.[^\']*)*\'/' > > i took these from the smarty compiler class. Has anyone started a commonly used PCRE Regexp library? It could be a simple wiki page or something (we had one on our old site that was getting quite large - but that is now gone - sigh) Anyway, is anyone up for that? These things come up pretty often, and having a library of common ones would save us all a lot of effort in these types of threads. Something like: Purpose: regex : Author : Thoughts? We could use http://fsiu.uwc.ac.za/index.php?module=wiki&action=view_page&name=RegularExpressionMethods for it. --Paul All Email originating from UWC is covered by disclaimer http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Match anything between two " that is not a " except if it is escaped...
This is for double quotes: '/"[^"]*(?:.[^"]*)*"/' this is for single: '/\'[^\']*(?:.[^\']*)*\'/' i took these from the smarty compiler class. "mathieu leddet" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] 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. ---8<--- - $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 = '#"([^"]*)"#'; // surround matching string with HTML span code to highlight $replacement = '"${1}"'; // perform the reg exp replacement $out = preg_replace($pattern, $replacement, $in); ---8<--- - $out contains : this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" This behaviour is normal considering my pattern (anything between two " that is not a "). But I don't know how to get this : this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" I would like my pattern to express : Anything between two " that is not a " except if it is escaped. Thanks for reading me, any help in return is welcome ! -- Mathieu -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Match anything between two " that is not a " except if it is escaped...
Max Antonov schreef: 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 : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" . -- Mathieu If I right understand you scope. (yes, my English is bad) better than our russian. You hope to get result such as: this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" 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: [EMAIL PROTECTED]:~$ cat preg.php "${1}"'; // perform the reg exp replacement $out = preg_replace($pattern, $replacement, $in); echo $out,"\n\n"; [EMAIL PROTECTED]:~$ php preg.php this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" 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
[PHP] Re: Match anything between two " that is not a " except if it is escaped...
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 : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" . -- Mathieu If I right understand you scope. (yes, my English is bad) You hope to get result such as: this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" 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: [EMAIL PROTECTED]:~$ cat preg.php "${1}"'; // perform the reg exp replacement $out = preg_replace($pattern, $replacement, $in); echo $out,"\n\n"; [EMAIL PROTECTED]:~$ php preg.php this is a string : "Hi everyone my name is \"Mathieu\"!" Here is a second string : "PHP is just perfect" 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