通过匹配字符串来选择整个段落

时间:2015-06-23 16:20:00

标签: python regex nltk text-mining

假设我在阅读文件时有两个段落:

'Baa, baa, black sheep, have you any wool?
Yes sir, yes sir, three bags full!
One for the master
One for the dame'

'Mary had a little lamb,
its fleece was white as snow;
And everywhere that Mary went,
the lamb was sure to go.'

是否有任何代码(使用正则表达式或其他内容),如果我搜索'lamb',会选择整个第二段?

2 个答案:

答案 0 :(得分:0)

假设这些段落都在一个字符串中,这样的东西应该起作用:

strace

答案 1 :(得分:0)

这将选择包含function calculate(num1, num2) { var a = parseInt(num1, 2); var b = parseInt(num2, 2); return a + b; } calculate('101', '10') //Returns 7

的段落
lamb

DEMO

这是python代码:

([^\']*(?=lamb)[^\']*)

import re data = """ 'Baa, baa, black sheep, have you any wool? Yes sir, yes sir, three bags full! One for the master One for the dame' 'Mary had a little lamb, its fleece was white as snow; And everywhere that Mary went, the lamb was sure to go.' """ match = re.search('([^\']*(?=lamb)[^\']*)',data) print(match.group())

Output: