preg_match and where do my parenthesis go?

Dear Members,

Each line ends with a dollar amount (although some lines have a dollar amount in the middle).

Here is a line:
$string = ‘123456 JONES: Spirit of the Ocean, originally 5.00 with coupon. 200 Pages 4.99 (used)’;

NON-working code:
preg_match("/^\d{6}.?[A-Z+]:.\d*.\d{2}/",$string,$match);
print_r($match);

How do I match the middle (everything between the colon “:” and the price closest to “end-of-line”)?
Spirit of the Ocean, originally 5.00 today. 200 Pages

How do I match just the price?
4.99

If I put parentheses where I think they should go, I get a bad result:
preg_match("/^\d{6}.?[A-Z+]: (.) (\d*.\d{2})/",$string,$match);
print_r($match);

$match[1] => Spirit of the Ocean, originally 5.00 with coupon. 200 Pages 4 (lops off the “.99”)
$match[2] => .99 (lops off the ‘4’)

I’ve tried shifting around the parentheses of no avail. Please help?

Thank you!!

I’m not that good with regex…

[php]
$string = ‘123456 JONES: Spirit of the Ocean, originally 5.00 with coupon. 200 Pages 4.99 (used)’;

if (preg_match("/Pages (\d+)(.\d{2})/", $string, $matches)) {
echo $matches[1] . $matches[2];
}[/php]

Thank you for your attempt at Regex. It looks like your skills are about the same as mine; non-existant.

If anyone out there can help me with my original question (how to capture everything between the colon : and the dollar amount), I’d sure appreciate it.

I don’t know why my code lops off the “4” in one, and doesn’t include it in the other.

Please help.

Thank you.

I read the question wrong, I thought you were after the price only.

If you are after this portion:

Spirit of the Ocean, originally 5.00 with coupon. 200 Pages 4.99 (used)

An alternative, not using regex is this,
[php]
$val = substr($string, strpos($string, “:”) +1);
echo $val;[/php]

You can modify it to get s subset of string values.

Hm, one day I must learn this magic that is regular expressions

My best attempt, I think it solves your problem

[php]$string = ‘123456 JONES: Spirit of the Ocean, originally 5.00 with coupon. 200 Pages 4.99 (used)’;

if (preg_match("/^\d*\s\w*:\s(.*)(\d.\d{2})\s(used)$/", $string, $matches)) {
print_r($matches);
}[/php]

Array ( [0] => 123456 JONES: Spirit of the Ocean, originally 5.00 with coupon. 200 Pages 4.99 (used) [1] => Spirit of the Ocean, originally 5.00 with coupon. 200 Pages [2] => 4.99 )

Rundown
line start
digits of any length
single whitespace
word of any length
single colon
single whitespace
any string (capture this)
decimal (digit, single dot, two digits) (capture this)
single whitespace
(used)
line end

To make the end more generic (not hard code “used” and make the entire (used) thing optional

^\d*\s\w*:\s(.*)(\d\.\d{2})\s?\(?\w*\)?$

Also, if the format is always exactly the same, you can just explode the string using spaces, place into an array
and just grab the needed values that way. Depends a lot on how stable the input is. Do you know it is the
same format all the time?

Thank you all for your help. Astonecipher, I like your spirit, you keep trying until you get it. I need to be more like that. JimL, thank you for your code suggestion, I will try that out tonight. You are right about the “used” portion – that is optional. ErnieAlex, you made an interesting point. I could explode it and go word by word. Thank you all for your different takes on this problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service