Please help with this reg expression.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Please help with this reg expression.

    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:

    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 ?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    no, but you can try var_dump($match es);

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Thanks,

      I inserted that code, plus an echo to show
      the contents of $prod->price.
      like this:

      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.

      ???


      .

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you’re not looking for "then after" (as a string), you’re looking for one of the characters of " <=aefhnrt"

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Your regex is to blame. It finds the shortest match, eg the match without either day or month.
          (day)?(month)?
          I think you want:
          ((day)|(month))

          Comment

          • jeddiki
            Contributor
            • Jan 2009
            • 290

            #6
            Yes,

            I think you are right :-)

            Actually, do I need the double parenthesis ?

            Maybe just (day|month)? ?

            Oh - tried it and I had to take out that ?

            (day|month) seems to work.

            Thanks for helping. :)


            .

            Comment

            Working...