Home > Back-end >  How do I make a regex take the word before something but not grab everything that is behind the patt
How do I make a regex take the word before something but not grab everything that is behind the patt

Time:12-31

import re, random

input_t = str(input())
input_text_to_check = input_text.lower()

regex_pattern_01 = r"\s*\¿?(?:how many|how much)\s*((?:\w \s*) )?\s*\¿?(?:is there|are there|is in|are in|does he have|does she have|do you have|do we have| am | is | are |\?)\s*"

n = re.search(regex_pattern_01, input_text_to_check, re.IGNORECASE)

if n:
    accounting_object, = n.groups()
    accounting_object = accounting_object.strip()

    if(accounting_object == ' ' or accounting_object == ''):
        print("I think you haven't told me what you mean")

    print(accounting_object)

I need to extract in a character string, 'the noun together with its adjectives in case of having them'. For example, for example in these cases this should extract the following words:

how much milk is there? ---> 'milk'

How many seeds are in the bag? ---> 'seeds'

How many antique wooden chairs are in the attic? ---> 'antique wooden chairs'

How many 1 meter rulers does he have? ---> '1 meter rulers'

how many nuts are required? ---> 'nuts'

how many old clocks ---> It does not enter the if

how many old clocks are there ---> It does enter the if, and give 'old clocks'

how many old clocks are there? ---> It does enter the if, and give 'old clocks'

how should I fix this regex? Because it gives me back, for example: 'antique wooden chairs are in the attic' and not 'antique wooden chairs'

CodePudding user response:

The group in which you're looking ((?:\w \s*) ) for the nouns is "greedy." It tries to match as much as possible and thus consumes the rest of the sentence. You can make it non-greedy by adding a ? after the .

I assume you'd also want to make the last space non-greedy so that it won't capture a space after the noun. Then it would be: (?:\w \s*?) ?.

Played around with the regex here.

CodePudding user response:

Why you don't write few conditions for every case, for example in this case, How many and are

import re
x = 'How many antique wooden chairs are in the attic?'
p = re.compile(r'How many\s*(.*)are')
m = p.search(x)           # Run a regex search anywhere inside a string
if m:                     # If there is a match
    print(m.group(1))     # Print Group 1 value

Output will be

antique wooden chairs
  • Related