Jump to content

[SOLVED] Very confusing Regex problem


lococobra

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.