【发布时间】:2020-07-16 05:41:27
【问题描述】:
我很久没有使用正则表达式了,不知道如何匹配一个实际的美元符号和任何对美元符号的引用,正则表达式告诉我特殊含义和情况。我需要匹配$。我预计 \$ 或 $$ 应该逃脱它,但我仍然不匹配它。
这是我的文字
(WW) Capacity Charge
. . . . . . . . . . . . . . . . $ 123.45
WW Commodity Charge . . . . . . . . . $ 67.89
我正在尝试捕获123.45 我想我应该只匹配第一次出现的一些字符夹在美元符号、空格和换行符之间的位置。以下是我尝试过的一些正则表达式。
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 regex preg-match