Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to pick up comma and period Python

Tags:

python

regex

I wanted to create a regex which could match the following pattern:

5,000
2.5
25

This is the regex I have thus far:

re.compile('([\d,]+)')

How can I adjust for the .?

like image 518
James Hallen Avatar asked Jan 18 '26 16:01

James Hallen


1 Answers

Easiest method would just be this:

re.compile('([\d,.]+)')

But this will allow inputs like .... This might be acceptable, since your original pattern allows ,,,. However, if you want to allow only a single decimal point you can do this:

re.compile('([\d,]+.?\d*)')

Note that this won't allow inputs like .5—you'd need to use 0.5 instead.

like image 155
p.s.w.g Avatar answered Jan 20 '26 07:01

p.s.w.g



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!