I want to check if a string respects a format that i give as input, for example:
From a list of strings i want to extract the one that has the following format:
***.***.***
where the *
are all numbers.
I searched about regex but could not understand well enough to implement it.
This should do the trick. The regex string is ^[0-9]+\.[0-9]+\.[0-9]+$
Where I match each digit exactly 3 times, and check if a '.' separator is in the middle. ^ and $ signify the start and end of the string
>>> import re
>>> re.match('^[0-9]{3}\.[0-9]{3}\.[0-9]{3}$','111.222.333')
<_sre.SRE_Match object at 0x10f98cb28>
>>> re.match('^[0-9]+\.[0-9]+\.[0-9]+$','a11.22.33b')
>>> re.match('^[0-9]+\.[0-9]+\.[0-9]+$','1a1.22.3b3')
>>> re.match('^[0-9]+\.[0-9]+\.[0-9]+$','11.2a2.33')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With