Home > Software engineering >  python find specific pattern of text from string
python find specific pattern of text from string

Time:02-04

I would like to extract specific type of text from string.

Luxyry 2 bedroom apartment
Deluxe apartment 2 bedroom
Super luxyry 3 bedroom apartment
1 Bedroom studio apartment

This is the text I have and I want to extract 1 Bedroom or 2 bedroom or 3 bedroom from the text. The pattern will be the same like {no_of_bedroom} bedroom.

How to extract this in python ?

CodePudding user response:

You can use regex like the below:

import re
text = """
Luxyry 2 bedroom apartment
Deluxe apartment 2 bedroom
Super luxyry 3 bedroom apartment
1 Bedroom studio apartment
"""

res = re.findall(r'\d  [Bb]edroom', text)
print(res)

# Use 'set()' if you want unique values
# print(set(res))
# {'3 bedroom', '1 Bedroom', '2 bedroom'}

Output:

['2 bedroom', '2 bedroom', '3 bedroom', '1 Bedroom']

Explanation:

  • \d :
    • \d : Matches a digit (equivalent to [0-9])
    • : Matches the previous token between one and unlimited times
  • [Bb] : Match a single character present in the list below [Bb]

CodePudding user response:

You can make use of re module

#pip install re

Import re
text = 'Luxyry 2 bedroom apartment
        Deluxe apartment 2 bedroom
        Super luxyry 3 bedroom 
           apartment 1 Bedroom studio apartment'
Result = re.findall(r"\d \s\[Bb]bedroom", text)
Print(f"Result :{Result}")

\d will match 1 or more digits.

  • Related