Python 正则表达式匹配以句点结尾的段落
我有一系列可以采用这种格式的文档:Python 正则表达式匹配以句点结尾的段落,python,regex,Python,Regex,我有一系列可以采用这种格式的文档: Diagnosis of one of the following: A) Neovascular (wet) age-related macular degeneration OR B) Macular edema following retinal vein occlusion, OR C) Diabetic macular edema OR D) Diabetic retinopathy in patients with diabetic macular
Diagnosis of one of the following: A) Neovascular (wet) age-related
macular degeneration OR B) Macular edema following retinal vein
occlusion, OR C) Diabetic macular edema OR D) Diabetic retinopathy in
patients with diabetic macular edema. More text here.
PA Criteria
Criteria Details
Eylea (s)
Products Affected
EYLEA
Exclusion
Criteria
Required
Medical
Information
Age Restrictions
Prescriber
Restrictions
Coverage
Duration
Other Criteria
Off Label Uses
12 months
Indications
All Medically-accepted Indications.
Formulary ID 20276, Version 12
101
我想匹配(然后删除)以句号结尾的段落中的所有文本。因此,我想删除
Diagnosis of one of the following: A) Neovascular (wet) age-related
macular degeneration OR B) Macular edema following retinal vein
occlusion, OR C) Diabetic macular edema OR D) Diabetic retinopathy in
patients with diabetic macular edema.
及
我试过这样的方法:
\n\n[\s\S]*?[.][\n\n]
但是我想说的是\n\n不可能存在于
[\s\S]*?
我该怎么做
谢谢 您可以使用以下任一正则表达式来完成此操作 选择1 此选项使用
re.DOTALL
工作原理:
匹配以下任一项:(?:\A |\n{2})
在字符串开头断言位置(不同于\A
-在行的开头断言位置)^
匹配两个连续的换行符\n{2}
匹配任何字符,但无法匹配两个连续的换行符(?:(?!\n{2})。+
按字面意思匹配\。
前瞻匹配以下任一项(断言匹配项后面的内容,但不在结果中包含匹配项):(?=\n{2}|\Z)
匹配两个连续的换行符\n{2}
与\Z
相反-在字符串末尾断言位置(不同于\A
-在行末尾断言位置)$
匹配任何字符(除了(?:.|\n(?!\n))+
,因为\n
不匹配换行符),或者
如果后面没有另一个\n
\n
re.DOTALL
选项
工作原理(同样,基本相同,只是解释了区别):
匹配以下一次或多次,但尽可能少((?:\n{2}(*SKIP)(*FAIL)|)+?
-惰性量词)+?
匹配两个连续的换行符,然后使其失败(\n{2}(*SKIP)(*FAIL)
就像魔法一样防止正则表达式回溯到其当前位置,然后使当前匹配失败。简单地说,这将跳过所有匹配到(*SKIP)(*FAIL)
左侧的字符(直到并包括(*SKIP)
),然后在该位置之后继续模式匹配(有关更多信息,请参阅)\n\n
- 这里有一个不需要任何模块的简单解决方案:
doc = '...'
ps = '\n\n'.join([p for p in d.split('\n\n') if not p.endswith('.')])
ps = '\n\n'.join([p for p in d.split('\n\n') if not p.endswith('.') and p.strip()])
这将产生与原件完全相同的格式
如果您希望它更整洁:
doc = '...'
ps = '\n\n'.join([p for p in d.split('\n\n') if not p.endswith('.')])
ps = '\n\n'.join([p for p in d.split('\n\n') if not p.endswith('.') and p.strip()])
((.+\n)*(.\.\n))
应该可以做到这一点-
(.+\n)
捕获包含1个或多个字符的行(包括换行符)
(.+\n)*
执行零次或多次
((.+\n)*(..\.\n))
并包括一行零个或多个字符,以句点结尾,然后换行您是否尝试过使用(.+?)\.
?@Anwarvic和re.DOTALL
标志,我认为这可能会起作用。您可能需要正则表达式(?:\a |\n{2})(?:(?:!\n{2})。+(?:!\n{
与re.DOTALL
-看到了吗?你是想在删除后保留所有空白,还是想删除每个段落?文本句子中的所有句点都是结束符吗?与“琼斯博士推荐无花果作为均衡饮食的一部分”不同?
doc = '...'
ps = '\n\n'.join([p for p in d.split('\n\n') if not p.endswith('.')])
ps = '\n\n'.join([p for p in d.split('\n\n') if not p.endswith('.') and p.strip()])