Python 找出句子中的句型
我试图使用Python提供的Python 找出句子中的句型,python,regex,Python,Regex,我试图使用Python提供的re模块,但我认为我遗漏了一些东西。我想做的是,如果一个给定的模式在一个句子中,我能说,例如: 我有一句话“今天的天空是蓝色的” 我想知道模式blu*tod*是否在句子中 我尝试了很多东西,比如: data = 'The sky is blue today' pattern = 'blu*\stod* re.match(pattern,data,re.IGNORECASE) // doesn't worked pattern = [blu+]\s[tod+] re.
re
模块,但我认为我遗漏了一些东西。我想做的是,如果一个给定的模式在一个句子中,我能说,例如:
我有一句话“今天的天空是蓝色的”
我想知道模式blu*tod*
是否在句子中
我尝试了很多东西,比如:
data = 'The sky is blue today'
pattern = 'blu*\stod*
re.match(pattern,data,re.IGNORECASE) // doesn't worked
pattern = [blu+]\s[tod+]
re.search(pattern,data) // Match everything in my sentence, even if the pattern isn't inside
有人能帮我吗?或者给我指一个关于在句子中查找正则表达式的好教程
谢谢。你想了解贪婪和懒惰的区别,以及如何使用圆点。我喜欢这个网站:
您误解了
*
和+
在正则表达式中的作用。它们不像文件globbing中的通配符:它们分别是前一个字符的“0或更多”或“1或更多”。所以它在寻找类似“bluuuuuuuuuuuuutoddddd”的东西
另一个问题是,您交替使用match
和search
,但实际上match
只会从字符串的开头匹配。您需要在此处搜索
因此,您的代码可能应该是:
data = 'The sky is blue today'
pattern = 'blu.+\stod.+'
re.search(pattern,data)
虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生变化,只有链接的答案可能会失效。他要求的是一个好的教程,而一个好的教程,IMHO,必须以链接的形式出现。我也不想给他答案。