Duarte Andrade
Duarte Andrade

Reputation: 69

Check if string has a certain format

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.

Upvotes: 4

Views: 10556

Answers (3)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

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')

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

We can try using re.findall with the following pattern:

\b\d{3}\.\d{3}\.\d{3}\b

Sample code:

input = "here some number 123.456.789 for testing"
matches = re.findall(r'\b\d{3}\.\d{3}\.\d{3}\b', input)
print(matches)

['123.456.789']

Using re.findall is a good choice here, because it also means that you can capture more than one match in the input string.

Upvotes: 2

JonAmazon
JonAmazon

Reputation: 92

This is definitely a job for regex. A simple regex for that pattern might be

\d\d\d\.\d\d\d\.\d\d\d

The "\d" stands for any digit, and the "\." is an escaped period character (because "." is a special symbol in regex.) With python re library would probably use the findall method with that pattern,

list_of_matches = re.findall("\d\d\d\.\d\d\d\.\d\d\d", my_string)

Upvotes: 2

Related Questions