Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing numbers that are larger than 1 decimal place

Tags:

regex

php

I'm having problems with a regex which seems to be matching 1 decimal point numbers such as 1.20, 2.50, but not numbers such as 20.50 or 906.10

Here is the regex

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

I've also tried the following regex, but it seems to miss smaller numbers

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

Replacing d with [0-9] seems to not work

String

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 

Any help would be appreciated thank you.

For reference:

$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)
like image 663
mk_89 Avatar asked Dec 15 '25 13:12

mk_89


2 Answers

or this pattern w/ isg options

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

Demo
Explanation:

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)
like image 85
alpha bravo Avatar answered Dec 17 '25 12:12

alpha bravo


To match a decimal number from 0.00 to 9.99 use:

\b\d\.\d{2}\b
like image 28
Andie2302 Avatar answered Dec 17 '25 13:12

Andie2302



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!