匹配 php 正则表达式中的实际美元符号

Match an actual dollar symbol in php regex

我已经很长时间没有使用正则表达式了,无法弄清楚如何匹配实际的美元符号以及任何对美元符号和正则表达式的引用都会告诉我特殊含义和案例。我需要匹配一个 $。我预计 $$$ 应该逃脱它,但我仍然不匹配它。

这是我的文字

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

我正在尝试捕获 123.45 我想我应该只匹配第一个出现的地方,其中一些字符夹在美元符号之间,space和换行符。这是我尝试过的一些正则表达式。

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

只是为了检查我什至做过的事情是否匹配

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

如何匹配$和换行符之间的文本?

你在双引号中,所以你需要转义两次(一次用于 PHP,一次用于 PCRE)。不过我更喜欢字符 class,因为它适用于所有正则表达式风格。

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