import re
name = "John"
#In these examples it works fine
input_sense_aux = "These sound system are too many, I think John can help us, otherwise it will be waiting for a while longer"
#input_sense_aux = "These sound system are too many but I know that John can help us, otherwise it will be waiting for a while longer"
#input_sense_aux = "These sound system are too many but I know that John can help us. otherwise it will be waiting for a while longer"
#input_sense_aux = "Do you know if John with the others could come this afternoon?"
#In these examples it does not work well
#input_sense_aux = "John can help us, otherwise it will be waiting for a while longer"
#input_sense_aux = "Can you help us, otherwise it will be waiting for a while longer for John"
#input_sense_aux = "sorry! can you help us? otherwise it will be waiting for a while longer for John"
regex_patron_m1 = r"\s*((?:\w\s*)+)\s*?" + name + r"\s*((?:\w\s*)+)\s*\??"
m1 = re.search(regex_patron_m1, input_sense_aux, re.IGNORECASE) #Con esto valido la regex haber si entra o no en el bloque de code
if m1:
something_1, something_2 = m1.groups()
something_1 = something_1.strip()
something_2 = something_2.strip()
print(repr(something_1))
print(repr(something_2))
我需要像这样从regex到grab the content before "John":
(start of sentence|¿|¡|,|;|:|(|[|.) \s* "content for something_1" \s* John
个
然后:
John \s* "content for something_2" \s* (end of sentence|?|!|,|;|:|)|]|.)
个
在拳头示例中,正则表达式运行得很好:
'these teams are too many but I know that'
'can help us'
'Do you know if'
'with the others could come this afternoon'
但对于最后3个示例的情况,正则表达式不会返回任何内容
我需要帮助才能将我的正则表达式推广到所有这些情况,同时尊重它必须提取something_1
和something_2
的内容的条件
对于最后三个示例,预期结果为:
''
' can help us'
' otherwise it will be waiting for a while longer for '
''
' otherwise it will be waiting for a while longer for '
''