Altimus Prime
Altimus Prime

Reputation: 2327

Match an actual dollar symbol in php regex

I haven't used regex in a long time and can't figure out how to match an actual dollar symbol and any reference to dollar symbol and regex tells me about the special meanings and cases. I need to match a $. I expected that \$ or $$ was supposed to escape it, but I'm still not matching it.

Here's my text

(WW) Capacity Charge
. . . . . . . . . . . . . . . . $ 123.45
WW Commodity Charge  . . . . . . . . . $ 67.89

I'm trying to capture 123.45 I figured I should just match the first occurrence where some characters are sandwiched between the dollar symbol, space and a newline. Here are a few of the regexes I've tried.

preg_match("|(?<=\$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=\$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$$)(.*)(?=\n)|",$data[1],$matches); //no matches

Just to check that something matches I even did

preg_match("|(?<=\.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=1)(.*)(?=\n)|",$data[1],$matches); // 23.45

How can I match the text between the $ and the newline?

Upvotes: 1

Views: 344

Answers (1)

user3783243
user3783243

Reputation: 5224

You are in double quotes so you need to escape twice (once for PHP, then once for PCRE). I prefer a character class though because that works in all regex flavors.

(?<=[$]\s)(.*)(?=\n)

Upvotes: 1

Related Questions