2015-01-31 72 views
3

我在與這似乎是匹配1個小數點的數字,例如1.20,2.50正則表達式的問題,但不是數字,如20.50或906.10捕獲數是大於1位小數

這裏是正則表達式

/(?:site(?:\\.com)?[\\s\\w\\d^]*)(\\d+\\.\\d{2})/i 

我也試過以下的正則表達式,但它似乎錯過較小的數字

/(?:site(?:\\.com)?[\\s\\w\\d^]*)(\\d+\\d+\\d+\\.\\d{2})/i  

與更換d 0似乎不起作用

字符串

Debit card payment to site.com 
Germany 
on 01 May 1.30 
Debit card payment to site Germany 
on 01 May 4.63 
Debit card payment to site.Com 
Germany 
on 01 May 3.30 
Debit card payment to Paypal *Xiao 
Ref:- 23948 0000000000 32.98 
Debit card payment to site.Com 
Germany 
on 20 May 17.49 
Debit card refund from site.Com 
Germany 
on 21 May 429.29 

任何幫助,將不勝感激謝謝。

參考:

$re = "/(?:site(?:\\.com)?[\\s\\w\\d^]*)(\\d+\\.\\d{2})/i"; 
$str = "Debit card payment to site.com 
    Germany 
    on 01 May 1.30 
    Debit card payment to site Germany 
    on 01 May 4.63 
    Debit card payment to site.Com 
    Germany 
    on 01 May 3.30 
    Debit card payment to Paypal *Xiao 
    Ref:- 23948 0000000000 32.98 
    Debit card payment to site.Com 
    Germany 
    on 20 May 17.49 
    Debit card refund from site.Com 
    Germany 
    on 21 May 429.29 "; 
preg_match_all($re, $str, $matches); 
print_r($matches) 
+0

應該是預期的輸出? – 2015-02-01 00:55:46

+0

'/(?: site(?:\\。com)?)。*?\ b(\ d \。\ d {2})/ isg'應該這樣做。 – 2015-02-01 00:59:04

回答

1

或該圖案瓦特/ isg選項

site.*?\D\K(\d+\.\d{2}) 

Demo
說明:

site   # "site" 
.    # Any character except line break 
*?    # (zero or more)(lazy) 
\D    # <character that is not a digit> 
\K    # <Reset start of match> 
(    # Capturing Group (1) 
    \d   # <digit 0-9> 
    +    # (one or more)(greedy) 
    \.   # "." 
    \d   # <digit 0-9> 
    {2}   # (repeated {2} times) 
)    # End of Capturing Group (1) 
0

怎麼樣?

$re = "~(site.*?\s+)(\d+\.\d+)~mis"; 
1

要匹配的十進制數從0.00到9.99用途:

\b\d\.\d{2}\b 
0

只需添加一個字邊界\b在您的正則表達式:

$re = "/(?:site(?:\\.com)?[\\s\\w\\d^]*)\b(\\d+\\.\\d{2})/i"; 
//        here ___^^ 
$str = "Debit card payment to site.com 
    Germany 
    on 01 May 1.30 
    Debit card payment to site Germany 
    on 01 May 4.63 
    Debit card payment to site.Com 
    Germany 
    on 01 May 3.30 
    Debit card payment to Paypal *Xiao 
    Ref:- 23948 0000000000 32.98 
    Debit card payment to site.Com 
    Germany 
    on 20 May 17.49 
    Debit card refund from site.Com 
    Germany 
    on 21 May 429.29 "; 
preg_match_all($re, $str, $matches); 
print_r($matches) 

輸出:

Array 
(
    [0] => Array 
     (
      [0] => site.com 
    Germany 
    on 01 May 1.30 
      [1] => site Germany 
    on 01 May 4.63 
      [2] => site.Com 
    Germany 
    on 01 May 3.30 
      [3] => site.Com 
    Germany 
    on 20 May 17.49 
      [4] => site.Com 
    Germany 
    on 21 May 429.29 
     ) 

    [1] => Array 
     (
      [0] => 1.30 
      [1] => 4.63 
      [2] => 3.30 
      [3] => 17.49 
      [4] => 429.29 
     ) 

) 
0

你只需要改變一個字:

$re = "/(?:site(?:\\.com)?[\\s\\w\\d^]*)(\\d+\\.\\d{2})/i"; 

爲了

$re = "/(?:site(?:\\.com)?[\\s\\w\\d^]*?)(\\d+\\.\\d{2})/i"; 
            ^

*?代表非貪婪匹配,即,表達不嘗試僅多達必要儘可能但相匹配。這樣它就不會吃掉所有的數字。