Parsing text with regex in Python: determine match order and mapping value to object type?
Question: I'm attempting to use an 'estate description' field to obtain information
regarding different houses as an exercise for learning python. What I'm
interested in (what a lot of sites do _not_ show) is how many rooms there are
and, more importantly, how big each room/area is. I have the total number of
rooms per property (which was scraped separately), with which I'll be able to
guide to approximate how many rooms I'll allow per description.

The following examples are strings that occur and that I want to dissect. The
following strings are snippets that occur in sentences, as exemplified in the
first example.

    
    
    This wonderful apartment has a size of 250m2...which consists of a \                  
    Light living room (ca. 5,81 x 5,75 m)... the view from building is xxx and leads into the          
    ...Lovely kitchen with a lovely fridge (300x575)...  
    three bedrooms (5m2, 12m2 and 15m2)            
    two bedrooms (3,55 x 3,40 and 3,54 x 2,27 m)   
    a nice balcony (15, 15m2)                      
    beautiful outdoor space (20m2) 
    

The issue is as follows: getting most room sizes is not that difficult
utilizing the following regex:

    
    
    (\d{1,4}.{0,2}m[ .]?[2|xb2])|            # Finds 16m2
    ( \d{1,4}[.,]?\d{0,3}                    # Finds 3.90 x 3,00 m
      [ ]?[x]?[ ]\d{1,4}[.,]?\d{0,3}[ ?]m)|  #
    (\d{1,4}[ ]?x[ ]?\d{1,4})                # Finds 640x390
    

The issue I'm having is that twofold:

* How do I match them in order? i.e.: first find option A, then find option B? Observing the current behavior think regex prefers shorter strings over longer strings, and is not matching them from left to right. I could maybe do a regex, replace with nothing and then move on in a loop?
* Second: I have a hard time trying to envision how I'd be able to go from a list of matches with these room/area sizes to mapping them to the kind of area they describe (if possible). For example: 250m2 found => Look for a list of 'area types' (["bedroom", "kitchen", "apartment") in the previous ~ 20 characters or so in the past x words (going backwards), or perhaps via another regex looking back? 

I'm not expecting a 100% solution, I'm just trying to grasp how I'd go about
parsing such text and trying to obtain interesting info from blobs of text.
Any help is greatly appreciated.


Answer: First part of your question: If you intend to match them in order, you could
strictly sequence the regexes into something like a list.

    
    
    regexes = [re.compile(r'expression 1'), re.compile(r'expression 2'), ...]
    for regex in regexes:
      match = regex.search(input_text)
      if match: break
    
    if not match:
      print("We didn't find anything!")
    

Second part of your question: If you anticipate those blobs coming in similar
structure to the samples you've posted, perhaps isolate the portion of the
input string up to the point where a digit first appears and analyze just that
portion.

    
    
    fuzzy_regex = re.compile(r'^(\D*)(.*)$')
    (before_numbers, part_with_numbers) = fuzzy_regex.search(input_string).groups()
    # Analyze before_numbers to see what kind of room, etc.
    # Analyze part_with_numbers to see square footage, etc.
    

A more concrete example:

    
    
    >>> import re
    >>> def dissect(text):
    ...   return re.search(r'^(\D*)(.*)$', text).groups()
    ... 
    >>> dissect('The entire apartment is 250m2')
    ('The entire apartment is ', '250m2')
    >>> dissect('three bedrooms (5m2, 12m2 and 15m2)')
    ('three bedrooms (', '5m2, 12m2 and 15m2)')
    

