1

This is my input text: "you have a choice between 1, 2 or 3 bedrooms"

I want to get the number of bedrooms, so one or more numbers before "bedroom" (allowing: ',', '-', 'and', '&', 'or', and 'whitespace' between numbers)

I have tried this pattern: (1|2|3|4|5|6|,|-|\s|&|and|or){1,12}bedroom on regex101 and it works fine.

But my Python code below, does not work:

text = "you have a choice between 1, 2 or 3 bedrooms"
number_range_pattern = r"(1|2|3|4|5|6|,|-|\s|&|and|or){1,12}"
bedrooms = re.search(number_range_pattern + r"bedroom", text)
if bedrooms and len(bedrooms.groups()) >= 1:
    match = bedrooms.group(1) # <-- match is a whitespace

Result: match is whitespce

I want the result to be: "1, 2 or 3"

2 Answers 2

1

Here is a working solution:

text = "you have a choice between 1, 2 or 3 bedrooms"
m = re.search(r'\d+(?:,? (?:(?:and|or|&) )?\d+)*', text)
if m:
    print(m.group())  # 1, 2 or 3

The regex pattern here could use an explanation:

\d+                   match a number
(?:
    ,?                optional comma separator
    [ ]               space
    (?:
        (?:and|or|&)  and, or, & conjunction
        [ ]           followed by space
    )?                and/or/& zero or one time
    \d+               another number
)*                    zero or more times
Sign up to request clarification or add additional context in comments.

Comments

0

You need to print(bedrooms.group(0)) instead of bedrooms.group(1)

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.