View Full Version : Having trouble with this regex ...

jeddi

02-24-2010, 10:06 AM

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:

// 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 ?

.

Fumigator

02-24-2010, 04:26 PM

Test and tweak your regex here:

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

I tried yours and it 'aint workin.

jeddi

02-24-2010, 04:29 PM

Hi,

To try and highlight my error,

I inserted that code, plus an echo to show

the contents of $prod->price.

like this:

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 ?

.

MattF

02-24-2010, 05:11 PM

Everything in parantheses is captured separately. Adjust to suit.

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

jeddi

02-26-2010, 09:14 AM

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:

$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:

// 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

.

MattF

02-26-2010, 02:24 PM

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).

jeddi

02-26-2010, 03:19 PM

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.

// 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 ?

.

jeddi

02-26-2010, 04:10 PM

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 <br> Then $99.90 for 1 month

Free for the first 1 month <br> 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 :) )

.

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.