2017-02-24 84 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)