捕获大于1位小数的数字

时间:2015-01-31 21:37:11

标签: php regex

我遇到了正则表达式的问题,它似乎匹配1个小数点数字,如1.20,2.50,但不是20.50或906.10之类的数字

这是正则表达式

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

我也试过以下正则表达式,但它似乎错过了较小的数字

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

d替换[0-9]似乎无法正常工作

字符串

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)

5 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

要匹配从0.00到9.99的十进制数,请使用:

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

答案 2 :(得分:0)

怎么样?

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

答案 3 :(得分: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
        )

)

答案 4 :(得分:0)

您只需更改一个字符:

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

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

*?代表非贪婪匹配,即表达式尽量不尽可能匹配,但只能根据需要进行匹配。这样它就不会吃掉所有数字。