Home > Net >  How to create a regex for number with at least 3 numbers and 2 decimals or more?
How to create a regex for number with at least 3 numbers and 2 decimals or more?

Time:11-04

I want a regex that accept for example:

22.23566556 #Two digits before decimal point, and two or more decimals, accepted 
123.123 #Three digits before decimal point, and two or more decimals, accepted
1.00 #One number before decimal point, and two decimals, accepted
...

And non accepted numbers:

4564.1546 #Four digits before decimal point, not accepted
123.1 #Three digits before decimal point, but only one decimal, not accepted
...

I tried with:

import re

text = "212.12454"

result = re.search(r"\b\d{1,3}\.\d{2}?", text)
print(result.group())

But it return in console the number with two decimals:

212.12

And my expected output would be the whole number:

212.12454

I hope someone could help me, thanks!

CodePudding user response:

Since the lower bound is 2 and upper bound does not matter {2,} can be used Try,

import re

text = "212.12454"

result = re.search(r"\b\d{1,3}\.\d{2,}", text)
print(result.group())

CodePudding user response:

At last, you've typed only {2} which means it'll check for only two occurences. If you want 2 or more occurences, then you've to type {2,} which means it'll check for two or more

  • Related