SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression negative lookahead

    hello,

    nothing is working today please someone take a look at my code and tell me what's wrong with it.

    Code:
    $PregPattern = "([^`]*?)&(?!Std_Country|Std_State|Std_City|Start|View|PageIndex)=([^`]*?)";
    $PregReplace = "$1&$MyPrefix_$2=$3";
    $link = preg_replace("/$PregPattern/si",$PregReplace,$link);
    I'm trying to match everything NOT among those key strings (in a querystring) and put a prefix to it.

    what's wrong with it?

    thank you very much

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Can we get some sample data? And what the Output should be?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sure

    string to match:

    CatID=1&Std_Country=USA&Std_State=NY&Std_City=NY&Cuisine=chinese

    $MyPrefix = "Restaurants_";

    out of those keys, only cuisine should be replaced with Restaurants_Cuisine

    thanks

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    <?php
     
    $skip = array('Std_Country', 'Std_State', 'Std_City', 'Start', 'View', 'PageIndex', 'CatID');
    $data = 'CatID=1&Std_Country=USA&Std_State=NY&Std_City=NY&Cuisine=chinese';
    $prefix = 'MyPrefix';
     
    $r = preg_replace('/([a-z0-9]+)(?<!' . join('|', $skip) . ')=([\\w]+)/i', $prefix . '_\\1=\\2', $data);
     
    print $r; # CatID=1&Std_Country=USA&Std_State=NY&Std_City=NY&MyPrefix_Cuisine=chinese
    
     
    // ([a-z0-9]+)(?<!Std_Country|Std_State|Std_City|Start|View|PageIndex|CatID)=([\w]+)
    // 
    // Match the regular expression below and capture its match into backreference number 1 «([a-z0-9]+)»
    //    Match a single character present in the list below «[a-z0-9]+»
    //       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    //       A character in the range between "a" and "z" «a-z»
    //       A character in the range between "0" and "9" «0-9»
    // Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!Std_Country|Std_State|Std_City|Start|View|PageIndex|CatID)»
    //    Match either the regular expression below (attempting the next alternative only if this one fails) «Std_Country»
    //       Match the characters "Std_Country" literally «Std_Country»
    //    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «Std_State»
    //       Match the characters "Std_State" literally «Std_State»
    //    Or match regular expression number 3 below (attempting the next alternative only if this one fails) «Std_City»
    //       Match the characters "Std_City" literally «Std_City»
    //    Or match regular expression number 4 below (attempting the next alternative only if this one fails) «Start»
    //       Match the characters "Start" literally «Start»
    //    Or match regular expression number 5 below (attempting the next alternative only if this one fails) «View»
    //       Match the characters "View" literally «View»
    //    Or match regular expression number 6 below (attempting the next alternative only if this one fails) «PageIndex»
    //       Match the characters "PageIndex" literally «PageIndex»
    //    Or match regular expression number 7 below (the entire group fails if this one fails to match) «CatID»
    //       Match the characters "CatID" literally «CatID»
    // Match the character "=" literally «=»
    // Match the regular expression below and capture its match into backreference number 2 «([\w]+)»
    //    A word character (letters, digits, etc.) «[\w]+»
    //       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much. that has solved my problem.

    regards,


Bookmarks

Posting Permissions

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