假设我在文本文件的左右部分之间有一个制表符的以下字符串:
The dreams of REM (Geo) sleep The sleep paralysis
我要匹配上面的字符串,该字符串在另一个以下文件的每一行中同时匹配左部分和右部分:
^{pr2}$如果无法与填充字符串匹配,则尝试与子字符串匹配。在
我想用最左边和最右边的模式搜索。 例如(最左边的情况)
The dreams of REM sleep paralysis
The dreams of REM sleep The sleep
例如(大多数情况下):
REM sleep The sleep paralysis
The dreams of The sleep paralysis
再次感谢你的帮助。在
(好吧,你澄清了大部分你想要的。让我重申一下,然后澄清我在下面列出的仍然不清楚的要点。。。另外,把我给你看的入门代码改编一下,把结果发给我们。)
您希望逐行、不区分大小写地搜索一对匹配模式中每一个的最长连续匹配。所有的模式似乎都是不相交的(patternX和patternY不可能匹配,因为它们使用不同的短语,例如不能同时匹配“额叶”和“前额叶皮层”)。在
您的模式是以一系列对的形式提供的,('dom','rang'),=>;让我们通过它们的下标[0]和[1来引用它们,您可以使用字符串.拆分('\t')来获取它。) 重要的是匹配线必须匹配dom和rang模式(全部或部分)。 顺序是独立的,因此我们可以匹配rang然后dom,反之亦然=>;每行使用2个单独的正则表达式,并测试d和r是否匹配。在
模式有可选部分,在括号中=>;所以只需使用
(optionaltext)?
语法将其写入/转换为regex语法,例如:re.compile('Frontallobes of (leftside)? the brain', re.IGNORECASE)
返回值应该是迄今为止子字符串匹配最长的字符串缓冲区。在
现在有几件事需要澄清-请编辑您的问题,解释如下:
以上每一个问题都会影响解决方案,所以您需要为我们解答。当你只需要一些简单的东西时,写几页代码来解决最一般的情况是没有意义的。 一般来说,这被称为“NLP”(自然语言处理)。你可能最终会使用NLP库。在
到目前为止,准则的总体结构听起来像:
在您当前提供的7行输入上运行可以得到:
^{pr2}$相关问题 更多 >
编程相关推荐