Reputation: 149
$regex = '/(?<=(total: ))(\$\d+\.\d+)/';
$search = '$1.25 cart total: $24.99 $55.50';
preg_match($regex, $search, $price);
I would like to extract '$24.99' from the string above.
When I print_r the $price array, it is blank. According to this tool: http://regexr.com?2vfm9 my regex should be working correctly. When I run the code using only the "(\$\d+.\d+)" part of regex, the array returns all of the prices... so I know that part is working.
Am I implementing the positive lookahead incorrectly? Any other thoughts?
UPDATE: I think I might have found the problem:
The text that I am parsing has a "& nbsp;" after the colon. It wasn't showing when I displayed the raw text to myself, but was interfering with the regex. I'll update to confirm.
Upvotes: 1
Views: 2003
Reputation: 91488
To deal with
you could use:
$regex = '/(?<=(total:))[^$]+(\$\d+\.\d+)/';
$search = '$1.25 cart total: $24.99 $55.50';
preg_match($regex, $search, $price);
print_r($price);
output:
Array
(
[0] => $24.99
[1] => total:
[2] => $24.99
)
Upvotes: 0
Reputation: 8459
The problem is that your \
are not escaped:
$regex = '/(?<=(total: ))(\\$\\d+\\.\\d+)/';
Upvotes: 0
Reputation: 121790
You do not need capturing parentheses around total:
, in fact they are even detrimental in this case. They capture.
Try without them.
And by the way, what you are using here is a positive lookbehind, not lookahead, and I don't see why you use one at all?
Upvotes: 0