Solved

regex + price

Posted on 2004-03-31
6
218 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to display div value on input field value in php 11 54
PHP error function not working on AWS 10 75
MVC - procedural PHP 10 39
JQuery Search Filter 2 39
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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