gpt4 book ai didi

php - 捕获大于 1 位小数的数字

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:19 24 4
gpt4 key购买 nike

我在使用正则表达式时遇到问题,该正则表达式似乎匹配 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})  

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)

关于php - 捕获大于 1 位小数的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com