2017-02-24 91 views
0

我想是的后面是一个关键的词“量”正则表达式查找后面

preg_match_all("/amount.+\b(\d+(\.\d+)?)/im", $input_lines, $output_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.25,5023,60.25,700.25应该被捕获

+0

我想我想通拿出你想要的东西,但你应该真正解释数字应该与不应该匹配的逻辑。 – Theo

回答

2

你正在寻找的正则表达式是:amount\D+(\d+(?:\.\d+)?)\.?(?!\d)

看到它在这里的行动:https://regex101.com/r/iXwM40/1

这依赖于有是单词“量”和组数字之间没有号码。

这个关键是最后一组括号,它被称为负向预览:(?!\d)如果下面的字符是数字位,这将不匹配。 \d

查看向前看符号这里更多的信息:http://www.regular-expressions.info/lookaround.html

1

用下面的办法:

$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]); 

输出: 阵列

(
    [0] => 300 
    [1] => 400.25 
    [2] => 5023 
    [3] => 60.25 
    [4] => 700.25 
) 

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

\K - 重置报告的匹配的起点。任何先前消耗的字符都不再包含在最终的比赛

\d+(\.\d+)? - 所需要的数量(包括如果它是浮动的小数部分)

+1

如果没有前瞻(如我的回答),只要在字数和数字之间有另一个字符,它仍然会捕获最后一行中的数字。看到这里:https://regex101.com/r/i4uiEj/1 - 我喜欢使用\ K虽然:) – Theo

+0

@Theo,这不是OP发布的输入,它是不同的。否则,OP应该澄清这种情况 – RomanPerekhrest

+0

我同意OP可能会更清楚(我做的第一件事就是评论) - 但这些都是明显的例子,而且这显然将在不同的输入上运行。 – Theo

0

给匹配这样的尝试\bamount\b.*?(\d+(?:\.\d*)?|\.\d+)

\b amount \b .*? 
(       # (1 start) 
     \d+ 
     (?: \. \d*)? 
    | \. \d+ 
)        # (1 end)