Link to home
Start Free TrialLog in
Avatar of kuist
kuist

asked on

regex + price

Hi folks,

Here is some string I have to evaluate against a regex:

piece_1_Y_100_Motherboard
piece_2_Y_189_Processor
piece_7_N_0.1_OS
piece_8_N_0.1_Monitoring

The regex I use is:
ereg("piece_([0-9]+)_(Y|N)_([^_]+)_(.+)", $key, $regs)
Where:
// $regs[0] = $key = le nom de la variable au complet
// $regs[1] = position
// $regs[2] = Y ou N (inFormula)
// $regs[3] = price
// $regs[4] = Component's name

The problem I have is when the price contains a dot in it (like the last 2 string I gave above).
I only get 0
I tried changing ([^_]+) to ([0-9.]+) without luck

If someone could explain me what's wrong with this, I'd appreciate it.

Thanks.
Avatar of venkateshwarr
venkateshwarr

This works....

ereg("piece_([0-9]+)_(Y|N)_(((([0-9]+).([0-9]+))|([0-9]+) )*)", $key, $regs)
ASKER CERTIFIED SOLUTION
Avatar of venkateshwarr
venkateshwarr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kuist

ASKER

How do I access the price from ereg("piece_([0-9]+)_(Y|N)_(([0-9]+.[0-9]+)|([0-9]+))", $key, $regs) ??

thanks

<?php

//$msg = "piece_1_Y_100_Motherboard";
$msg = "piece_7_N_0.1_OS";

if (ereg ("piece_([0-9]+)_(Y|N)_(([0-9]+.[0-9]+)|([0-9]+))", $msg, $regs)) {
  echo "$regs[3] -- $regs[2] -- $regs[1]";
} else {
  echo "Invalid  format";
}

?>

<?php

//$msg = "piece_1_Y_100_Motherboard";
$msg = "piece_7_N_0.1_OS";

if (ereg ("piece_([0-9]+)_(Y|N)_(([0-9]+.[0-9]+)|([0-9]+))", $msg, $regs)) {
  echo "$regs[3] -- $regs[2] -- $regs[1]";
} else {
  echo "Invalid  format";
}

print "\nprice = $regs[3]\n";
?>
Avatar of kuist

ASKER

the price it gives me is 0_1 instead of 0.1

any idea why ?