如何使用regex解析python上的文本以从以下内容中提取有效的段落
近适应为一例。通过改变透镜的形状,调节调节耐火功率,使之与被观察物体的距离相适应。问题是
我想要提取
近适应是一种住宿情况。通过改变透镜的形状,调节调节耐火功率,使之与被观察物体的距离相适应。
这意味着有效的文本应该以句号结尾,并去掉诸如“问题是”之类的东西,这是一个未完成的句子,以及字符前面的任何东西,比如\n。
另一个例子是
<p>--神经末梢的多巴胺水平由单胺氧化酶控制,该酶使突触前的神经递质失活。</p>\n</body></html>
哪个应该提取
神经终末中的多巴胺水平由单胺氧化酶控制,该酶使突触前的神经递质失活。
因此,也要去掉html标记。
所以我需要一段时间结束的干净的通道。没有任何换行符或html标记,可以在相关段落之后或之前出现。所有的段落或多或少都像我提供的例子。
发布于 2018-06-02 17:18:20
发布于 2018-06-02 17:51:03
关键是要能够准确地说明以下条件:
在你的例子中,这些似乎是
由于正则表达式在默认情况下是贪婪,所以结束条件将适用于最长的匹配,因此得到不包含延续条件的多个句子。这给出了正则表达式A-Z+.
>>> import re
>>> matcher = re.compile('[A-Z][^\n<>]+\.')利用你所提供的:
>>> matcher.findall('''<p>The level of dopamine available in nerve terminals is controlled by the enzyme monoamineoxidase, which inactivates the neurotransmitter in the presynapse. </p>\n\n</body></html>''')[0]
'The level of dopamine available in nerve terminals is controlled by the enzyme monoamineoxidase, which inactivates the neurotransmitter in the presynapse.'
>>> matcher.findall('''near accomodation\n\nNear accomodation is one case of accomodation. By changing the shape of the lens, accomodation adjusts the refractory power to the distance of an object under observation. The issue is''')[0]
'Near accomodation is one case of accomodation. By changing the shape of the lens, accomodation adjusts the refractory power to the distance of an object under observation.'随时根据需要进行调整。
https://stackoverflow.com/questions/50659268
复制相似问题