Php 正则表达式查找关键字后面的数字

Php 正则表达式查找关键字后面的数字,php,regex,preg-match,preg-match-all,Php,Regex,Preg Match,Preg Match All,我试图捕获文本中的数字,后面跟一个关键字“amount” preg\u match\u all(“/amount.+\b(\d+(\.\d+))/im”,$input\u line,$output\u array) 我的输入数据是 here is some number 100.25 that does not 200. but this amount should be captured 300. and this amount should be captured 400.25 too an

我试图捕获文本中的数字,后面跟一个关键字“amount”

preg\u match\u all(“/amount.+\b(\d+(\.\d+))/im”,$input\u line,$output\u array)

我的输入数据是

here is some number 100.25
that does not 200.
but this amount should be captured 300.
and this amount should be captured 400.25 too
and this amount should be captured $5023 too
and this amount should be captured $60.25 too
and this amount should be captured 700.25.

But not this amount 800.25.2

因此,只应捕获数字300、400.255023、60.25700.25

您要查找的正则表达式是:
amount\D+(\D+(?:\。\D+)\。(?!\D)

请在此处查看它的实际操作:

这取决于单词“amount”和一组数字之间没有数字

关键是最后一组括号,称为负前瞻:
(?!\d)
如果以下字符是数字,则不匹配<代码>\d


请在此处查看有关lookaheads的更多信息:

您要查找的正则表达式是:
amount\D+(\D+(?:\。\D+)\。(?!\D)

请在此处查看它的实际操作:

这取决于单词“amount”和一组数字之间没有数字

关键是最后一组括号,称为负前瞻:
(?!\d)
如果以下字符是数字,则不匹配<代码>\d


请参阅此处有关lookaheads的更多信息:

使用以下方法:

$input_lines = "here is some number 100.25
that does not 200.
but this amount should be captured 300.
and this amount should be captured 400.25 too
and this amount should be captured $5023 too
and this amount should be captured $60.25 too
and this amount should be captured 700.25.

But not this amount 800.25.2";

preg_match_all("/(?:amount [^\d]+?)\K\d+(\.\d+)?/m", $input_lines, $matches);

print_r($matches[0]);
输出: 排列


(?:amount[^\d]+?)
-将字符串(行)与
amount
匹配,后跟除数字以外的任何字符

\K
-重置报告匹配的起点。任何以前使用的字符将不再包含在最终匹配中


\d+(\.\d+)
-匹配所需的数字(如果是浮点数,则包括小数部分)

使用以下方法:

$input_lines = "here is some number 100.25
that does not 200.
but this amount should be captured 300.
and this amount should be captured 400.25 too
and this amount should be captured $5023 too
and this amount should be captured $60.25 too
and this amount should be captured 700.25.

But not this amount 800.25.2";

preg_match_all("/(?:amount [^\d]+?)\K\d+(\.\d+)?/m", $input_lines, $matches);

print_r($matches[0]);
输出: 排列


(?:amount[^\d]+?)
-将字符串(行)与
amount
匹配,后跟除数字以外的任何字符

\K
-重置报告匹配的起点。任何以前使用的字符将不再包含在最终匹配中


\d+(\.\d+)
-匹配所需的数字(如果是浮点数,则包括小数部分)

尝试一下
\bamount\b.*(\d+(?:\.\d*)?\.\d+)


尝试一下
\bamount\b.*(\d+(?:\。\d*)?\124;\。\ d+


我想我知道了你想要什么,但是你应该真正解释数字应该和不应该匹配的逻辑。我想我知道了你想要什么,但是你应该真正解释数字应该和不应该匹配的逻辑。没有前瞻性(如我的回答)只要在单词amount和数字之间有另一个字符,它仍然会捕获最后一行的数字。请看这里:-我喜欢使用\K:)@Theo,这不是OP发布的输入,而是不同的。否则,OP应该澄清此类案例。我同意OP本可以更清楚(我做的第一件事是评论),但这些都是明显的例子,这显然将在不同的输入上运行。没有前瞻性(如我的回答)只要在单词amount和数字之间有另一个字符,它仍然会捕获最后一行的数字。请看这里:-我喜欢使用\K:)@Theo,这不是OP发布的输入,而是不同的。否则,OP应该澄清此类案例。我同意OP本可以更清楚(我做的第一件事是评论),但这些都是明显的例子,这显然将在不同的输入上运行。
 \b amount \b .*? 
 (                             # (1 start)
      \d+ 
      (?: \. \d* )?
   |  \. \d+ 
 )                             # (1 end)