Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Having trouble with this regex ...

    Hi,

    I must have made a mistake somewhere with this one

    The strings I am processing have this type of format:

    " $7.00 now, then after 7 days $77.00 every 1 year"

    " $4.99 now, then after 3 days $99.90 every 1 month"

    "$7.00 now, then after 1 month $27.00 every 1 month"

    The initial payment is easy enough but I am having problems
    getting out the payment "gap_unit".

    By "gap_unit", I mean the "after 7 days" or "after 1 month"

    This is what I have:

    PHP Code:
    // Find prices - gap unit
    if(preg_match('#([<=then after])(day)?(month)?#'$prod->price$matches) == 1) {
      
    $price_gap_unit $matches[0];
      if(
    $price_gap_unit == ''$price_gap_unit $matches[1];
       
    write_log("$prod->prod_id. Price_gap_unit: $price_gap_unit\r\n");
       }
    else {
      
    $price_gap_unit 'none';

    In my log I get this:

    95330. Price_gap_unit:
    So, it looks like the regex is finding the match, but it is not
    in the $matches[0] or $matches[1] .

    Any ideas what is going wrong ?



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Test and tweak your regex here:

    http://www.spaweditor.com/scripts/regex/

    I tried yours and it 'aint workin.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,

    To try and highlight my error,
    I inserted that code, plus an echo to show
    the contents of $prod->price.
    like this:

    PHP Code:
    if(preg_match('#([<=then after])(day)?(month)?#'$prod->price$matches) == 1) {
       
    $price_gap_unit $matches[0];
       if(
    $price_gap_unit == ''$price_gap_unit $matches[1];
        
    write_log("$prod->prod_id. Price_gap_unit: $price_gap_unit \r\n");
        echo 
    "Prod->price: $prod->price<br>"
        
    var_dump($matches);
        exit;
        }
    else {
        
    $price_gap_unit 'none';
        } 

    Interestingly the output is:

    Prod->price: $49.95
    array(2) { [0]=> string(1) " " [1]=> string(1) " " }
    So - I thought that if stmt would only be true if "then after" was found in the string.

    Can anyone see where I have gone wrong ?


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Everything in parantheses is captured separately. Adjust to suit.

    Code:
    '#\$([\d\.]+) now, then after (\d+) (days|month) \$([\d\.]) every (\d+) (month|year)#'

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks Matt,

    The problem is that the "price" string is not constant in it's
    structure, so I can not use a 3-in-1 type of approach.

    My look-behind reg exes are working but I can not get the
    look-ahead one to work:

    PHP Code:
    $prod->price " $7.00 now, then after 7  days  $77.00 every 1 year ";

    // Calculate prices - gap
    if(preg_match('#([?<then after])(\d) #'$prod->price$matches) == 1) {
       
    $price_gap trim($matches[0]);
       
    write_log("$prod->prod_id. Price_gap: $price_gap\r\n");
       }
    else {
       
    $price_gap 0;
       }    

    // Calculate prices - gap unit
    if(preg_match('#([?<then after])(day|month)#'$prod->price$matches) == 1) {
       
    $price_gap_unit trim($matches[0]);
       
    write_log("$prod->prod_id. Price_gap_unit: $price_gap_unit \r\n");
       }
    else {
       
    $price_gap_unit 'none';
       }    

    // Calculate Future price         *** LOOK AHEAD  ***

    if(preg_match('#(\.\d)(?=every)#'$prod->price$matches) == 1) {
       
    $future trim($matches[0]);
       }
    else {
       
    $future 0;
       }    

    write_log("$prod->prod_id. Future: $future \r\n");

    $pricegap_flag $price_gap_unit[0];
    write_log("$prod->prod_id. Price_gap: $price_gap, Pricegap_flag: $pricegap_flag \r\n");

    if( 
    $pricegap_flag == 'd' && $price_gap 31) {
       
    $price_2nd $future;
       
    $earn = ($price_init+$price_2nd)*($comm/100);
       
    $totearn = ($price_init+$price_2nd+(2*$future) )*($comm/100);
       }
    else {
       
    $price_2nd 0;
       
    $earn $price_init*($comm/100);
       
    $totearn = ($price_init+$price_2nd+(2*$future) )*($comm/100);
       }    
            
    write_log("$prod->prod_id. Future: $future, Price_2nd: $price_2nd, Earn: $earn, Totearn: $totearn \r\n"); 
    The results from the log are:


    95330. Price_gap: 7
    95330. Price_gap_unit: day
    95330. Future: 0
    95330. Price_gap: 7, Pricegap_flag: d
    95330. Future: 0, Price_2nd: 0, Earn: 3.5, Totearn: 3.5


    So, you can see from the results that the
    "Price_gap", "Price_gap_unit" are getting found but the
    "Future" is not being picked up.

    So it looks like my look-ahead is not working

    Thats this one:

    PHP Code:
    // Calculate Future price
    if(preg_match('#(\.\d)(?=every)#'$prod->price$matches) == 1) {
       
    $future trim($matches[0]);
       }
    else {
       
    $future 0;
       } 
    Can you see what I did wrong ?

    Thanks




    .
    Last edited by jeddi; 02-26-2010 at 09:18 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by jeddi View Post
    The problem is that the "price" string is not constant in it's structure, so I can not use a 3-in-1 type of approach.
    Supply some real world examples of the variations of the string, (in several different formats it actually will appear in).

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    It will take a while to go through the file
    and did them out.

    Even if I can use a 3-in-1 or a 2-in-1 solution,

    I would still like to understand where I have gone wrong with this particular
    reg expression.

    Code:
    // Calculate Future price
    if(preg_match('#(\.\d)(?=every)#', $prod->price, $matches) == 1) {
       $future = trim($matches[0]);
       }
    else {
       $future = 0;
       }
    Why does it not match ?

    Ahh - I think there is a ) missing isn't there ?



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Matt,

    I have gone 10 % through the file and found these
    combinations so far:

    $167.00 every 30 days for 2 times

    $199.00

    $14.97 every 1 month

    $97.00 every 1 year

    $49.95 every 28 days

    $10.00 every 30 days for 12 times

    $277.00 every 30 days for 2 times

    $1.00 now, then after 7 days $49.95 every 30 days for 2 times

    Free for the first 3 days &lt;br&gt; Then $99.90 for 1 month

    Free for the first 1 month &lt;br&gt; Then $27.00 for 1 month
    So I think that I do need the "lookback" to every working.

    There will probably be more price structures as well ...

    ( please see previous post as well )


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

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