我有:
Rutsch是给rutterman提升他的鱼卵的
这是“芬尼根守灵”中的一句话。史诗般的谜语书中充满了这样的动机,比如“摘掉那顶白帽子”和“小费”,所有这些词都会根据你在书中的位置而变异成类似的发音单词。我想要的只是一种方法来找到这个特定主题的明显出现,IE
word1是用于word2ing his word3的
发布于 2010-09-09 22:07:26
import re
# read the book into a variable 'text'
matches = re.findall(r'\w+ is for \w+ \w+ing his \w+', text)发布于 2010-09-09 21:52:50
您可以使用Python中的正则表达式来完成此操作:
import re
pattern = re.compile(r'(?P<word>.*) is for (?P=word) (?P=word)ing his (?P=word)')
words = pattern.findall(text)这与您的示例不匹配,但与[word] is for [word] [word-part]ing his [word]匹配。加入调味料调味。您可以在re模块docs中找到更多详细信息。
发布于 2012-02-21 01:32:06
此解决方案适用于您的示例,而不是您的描述:只有第一个字母是头韵的:
pairs = re.findall(r'((.)\w* is for \2\w* \2\w*ing his \2\w*)', fin, re.IGNORECASE)
matches = [ p[0] for p in pairs ]要搜索与您的描述匹配的案例,只需替换(.)使用(\w+),并删除\w*的所有实例。
https://stackoverflow.com/questions/3677116
复制相似问题