In [ ]:
import re

In [ ]:
#help(re)

Extract numbers from a string


In [ ]:
s = "Maison 3 pièce(s) - 68.05 m² - 860 € par mois charges comprises"

In [ ]:
re.findall(r'\d+\.?\d*', s)

In [ ]:
re.findall(r'\b\d+\.?\d*\b', s)

Search patterns


In [ ]:
s = "Maison 3 pièce(s) - 68.05 m² - 860 € par mois charges comprises"

In [ ]:
if re.search(r'Maison', s):
    print("Found!")
else:
    print("Not found!")

In [ ]:
if re.search(r'Appartement', s):
    print("Found!")
else:
    print("Not found!")

In [ ]:
if re.match(r'Maison', s):
    print("Found!")
else:
    print("Not found!")

Search and capture patterns


In [ ]:
s = "Maison 3 pièce(s) - 68.05 m² - 860 € par mois charges comprises"

In [ ]:
m = re.search(r'\b(\d+) pièce', s)
if m:
    print(int(m.group(1)))
else:
    print("Not found!")

In [ ]:
m = re.search(r'\b(\d+\.?\d*) m²', s)
if m:
    print(float(m.group(1)))
else:
    print("Not found!")

In [ ]:
m = re.search(r'\b(\d+\.?\d*) €', s)
if m:
    print(float(m.group(1)))
else:
    print("Not found!")

In [ ]:
s = "Maison 3 PIÈce(s) - 68.05 m² - 860 € par mois charges comprises"

Without re.compile()


In [ ]:
m = re.search(r'\b(\d+) pièce', s, re.IGNORECASE)
if m:
    print(int(m.group(1)))
else:
    print("Not found!")

With re.compile()


In [ ]:
num_pieces = re.compile(r'\b(\d+) pièce', re.IGNORECASE)

m = num_pieces.search(s)
if m:
    print(int(m.group(1)))
else:
    print("Not found!")