這個問題在這里已經有了答案: 正則運算式在第一場比賽中停止 (9 個回答) 3 小時前關閉。
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,并給出了“舊時鐘”
我應該如何修復這個正則運算式?因為它給了我回報,例如:'古董木椅在閣樓'而不是'古董木椅'
uj5u.com熱心網友回復:
您在其中尋找 ( (?:\w \s*) ) 名詞的組是“貪婪的”。它嘗試盡可能多地匹配,從而消耗了句子的其余部分。您可以通過?在 .
我假設您還想讓最后一個空格變得非貪婪,這樣它就不會在名詞之后捕獲空格。然后就是:(?:\w \s*?) ?。
在這里玩正則運算式。
uj5u.com熱心網友回復:
為什么你不為每種情況寫幾個條件,例如在這種情況下,How many以及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
輸出將是
antique wooden chairs
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399285.html
上一篇:當子字串的某些部分不重要時,字串中的Python子字串
下一篇:C 通過函式傳遞引數
