Solved

regex + price

Posted on 2004-03-31
6
214 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:kuist
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10729030
This works....

ereg("piece_([0-9]+)_(Y|N)_(((([0-9]+).([0-9]+))|([0-9]+) )*)", $key, $regs)
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 125 total points
ID: 10729082

I think this is what you wanted......

ereg("piece_([0-9]+)_(Y|N)_(([0-9]+.[0-9]+)|([0-9]+))", $key, $regs)

0
 

Author Comment

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

thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10730242

<?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";
}

?>
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10730246

<?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";
?>
0
 

Author Comment

by:kuist
ID: 10733221
the price it gives me is 0_1 instead of 0.1

any idea why ?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now