捕获大于小数点后 1 位的数字


Capturing numbers that are larger than 1 decimal place

>我遇到了一个正则表达式的问题,它似乎匹配了 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    

[0-9]替换d似乎不起作用

字符串

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)

或带有isg选项的此模式

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

演示
解释:

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.00 到 9.99 之间的十进制数,请使用:

'b'd'.'d{2}'b

怎么样?

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

只需在正则表达式中添加单词边界'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
        )
)

你只需要改变一个字符:

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

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

*?代表非贪婪匹配,即表达式不会尝试尽可能多地匹配,而只是在必要时进行匹配。这样它就不会吃掉所有的数字。