lococobra Posted July 13, 2007 Share Posted July 13, 2007 First off, yeah... I know there's a regex section, but no one responded to my previous question that was over there, so here I am... So lets say I have some php code stored in a variable, I want to isolate all the strings in that code. Right now I'm using the following expression: <?php preg_match_all("/([\"'])(.*?)\\1/", $input, $strings); ?> However, if you look carefully there's a problem... What if it encounters a string like this "I read an article called \"How To Use Regex\" it was very good". Obviously there's going to be some massive butchering of that string. So what I need to do is make sure that the character directly before the end quote (\\1) is not a backslash (\\) But here's a major problem... double backslash in a regular expression is not the same as a double backslash in say a string. How do I do this? My best guess: <?php preg_match_all("/([\"'])(.*?)^\\?\\1/", $orig, $st_s); ?> Please help! Quote Link to comment https://forums.phpfreaks.com/topic/59764-solved-very-confusing-regex-problem/ Share on other sites More sharing options...
sasa Posted July 13, 2007 Share Posted July 13, 2007 try <?php $input ='"I read an article called \"How To Use Regex\" it was very good"'; echo $input; preg_match_all("/([\"'])(.*?[^\\\])\\1/", $input, $strings); print_r($strings); ?> Quote Link to comment https://forums.phpfreaks.com/topic/59764-solved-very-confusing-regex-problem/#findComment-297114 Share on other sites More sharing options...
lococobra Posted July 13, 2007 Author Share Posted July 13, 2007 Thank you so much, that works great. Actually, yours needed to be modified just a bit to make it work in 100% of cases... "/([\"'])(.*?[^\\\]?)\\1/" Is what I ended up using (note the ? after [^\\\]) That was required so that strings like "" wouldn't kill it. Quote Link to comment https://forums.phpfreaks.com/topic/59764-solved-very-confusing-regex-problem/#findComment-297135 Share on other sites More sharing options...
effigy Posted July 13, 2007 Share Posted July 13, 2007 With lookaround: '/(["\'])(.*?)(?<!\\\)\1/' Quote Link to comment https://forums.phpfreaks.com/topic/59764-solved-very-confusing-regex-problem/#findComment-297381 Share on other sites More sharing options...
lococobra Posted July 13, 2007 Author Share Posted July 13, 2007 effigy, that code does not work. Quote Link to comment https://forums.phpfreaks.com/topic/59764-solved-very-confusing-regex-problem/#findComment-297526 Share on other sites More sharing options...
lococobra Posted July 13, 2007 Author Share Posted July 13, 2007 Nevermind, it does... and I'm terribly confused by it... but it works. Quote Link to comment https://forums.phpfreaks.com/topic/59764-solved-very-confusing-regex-problem/#findComment-297549 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.