0

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 ?

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Be warned that you are using re which is part of standard library, whilst regex is external library
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.