1

I have some sentence like

  • "Apartment at Chennai has 4 rooms with a swimming pool"
  • "this apartment has 2/3 room with a coridor"

How to extract the only number before the word "room|rooms". looking for an answer which must be 4 and 2/3. The code i tried,

  room_found =re.findall(r"\d\s?\/?\d?\s?(?=(rooms)|(room))", str_arg)
  print(room_found)

This prints [('', '4 room')] and [('', '2/3 room')], but i am expecting only 4 and 2/3 to be printed.

Induraj PR
  • 284
  • 2
  • 9
  • Omitting the capture groups from the OP's pattern with also include the space after the room number `2/3 `. It will also match "4/ room" which I think is not desired. There is also no need for using room and rooms in the pattern. There are more corrections to this pattern than just the mentioned duplicates. – The fourth bird Nov 18 '20 at 12:28

3 Answers3

5

You can use

\d+(?:/\d+)?(?=\s?rooms?\b)

Explanation

  • \d+ Match 1+ digits (Or \d for a single digit)
  • (?:/\d+)? Optionally match / and 1+ digits
  • (?= Positive lookahead to assert what is directly at the right is
  • \s?rooms?\b Match an optional whitspace char followed by room or rooms
  • ) Close the lookahead

Regex demo

import re
str_arg = "\"Apartment at Chennai has 4 rooms with a swimming pool\" \"this apartment has 2/3 room with a coridor\""
 
room_found =re.findall(r"\d+(?:/\d+)?(?=\s?rooms?\b)", str_arg)
print(room_found)

Output

['4', '2/3']
abhigyanj
  • 2,355
  • 2
  • 9
  • 29
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

You can add a condition to find all the numbers before the word room and rooms.

And You can enclose all the valid values inside the square brackets like you are allowing forward slash apart from digits as well.

[\d\/]+ => It selects the digits and forward slash.

(?=\srooms?) => It selects the numbers before the word room and rooms.

[\d\/]+(?=\srooms?)

Please find demo here

Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28
0

You can use ([0-9]+) rooms. So your final code would look like:

import re

str_arg = "\"Apartment at Chennai has 4 rooms with a swimming pool\" \"this apartment has 2/3 room with a coridor\""


room_found = re.findall(r"([0-9]+) rooms", str_arg)
print(room_found)
abhigyanj
  • 2,355
  • 2
  • 9
  • 29