繁体   English   中英

如何让正则表达式在某事之前获取单词,但不获取该单词后面的模式背后的所有内容?

[英]How do I make a regex take the word before something but not grab everything that is behind the pattern that follows the word?

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)

我需要在字符串中提取“名词及其形容词以防万一”。 例如,例如在这些情况下,这应该提取以下单词:

有多少牛奶? --->'牛奶'

袋子里有多少种子? ---> '种子'

阁楼里有几把古董木椅? ---> '古董木椅'

他有多少个 1 米尺? ---> '1 米尺'

需要多少个坚果? ---> '坚果'

多少个旧时钟--->它没有进入if

有多少个旧时钟 ---> 它确实输入了 if,并给出“旧时钟”

有多少个旧钟? --->它确实进入了if,并给出'旧时钟'

我应该如何修复这个正则表达式? 因为它给了我回报,例如:'古董木椅在阁楼里'而不是'古董木椅'

为什么你不为每种情况写几个条件,例如在这种情况下, How manyare

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 将

antique wooden chairs

您在其中查找名词的组 ( (?:\w+\s*)+ ) 是“贪婪的”。 它尝试尽可能多地匹配,从而消耗句子的 rest。 您可以通过添加? +之后。

我假设您还想让最后一个空格不贪心,这样它就不会在名词之后捕获空格。 那么它将是: (?:\w+\s*?)+? .

这里玩正则表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM