Results 1 to 2 of 2

Thread: Regex Problem

  1. #1
    Join Date
    Mar 2010

    Exclamation Regex Problem

    Hi Everyone,

    Im having a little problem with a peice of regex im trying to write... im a profficent coder when it comes to HTML, CSS, PHP, JavaScript and AS2/AS3 but the one thing that still trips me up again and again is regex!

    Im trying to write a peice of regex that will be used with preg_replace to remove all "[" characters from a string as long as they don't appear within double (") or single (') quotes or a blackslash(\). When appearing in quotes it ([) could have any characters (a-zA-Z0-9/,.\;[]-_()*&^%$#@!`~<>?{}|) either side of it within the quotes. How would I do this? I have included the regex I tried to create to solve this problem but it doesn't quite work. I hope i'm on the right track...

    I feel im almost on the right track but im not quite there. Any and all help would be appreciated.



  2. #2
    Join Date
    Nov 2008
    Someone with better regex skills can probably come up with a one liner, but here's how I'd do it:

    PHP Code:
    $matches = (preg_split('/([\'"].+?[\'"])/'$string, -1PREG_SPLIT_DELIM_CAPTURE));
    $new '';
    foreach (
    $matches as $match) {
        if (
    strpos($match'"') === || strpos($match"'") === 0) {
    // stuff inside quotes can be handled here
    $new .= $match;
    // stuff outside quotes can be handled here
    $new .= str_replace('['''$match);

    Last edited by Mindzai; 03-24-2010 at 05:48 AM.
    The first rule of Tautology Club is the first rule of Tautology Club.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center