我需要根据括号中的字母数检索首字母缩略词的定义。对于我正在处理的数据,括号中的字母数对应于要检索的单词数。我知道这不是一个可靠的方法来获得缩写,但在我的情况下,它会的。例如:
尽管家庭健康史(FHH)被普遍认为是常见慢性病的一个重要危险因素,但很少被护士(NP)考虑
期望输出:家庭健康史(FHH)、执业护士(NP)
我知道如何从字符串中提取括号,但之后我就卡住了。感谢任何帮助。在
import re
a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'
x2 = re.findall('(\(.*?\))', a)
for x in x2:
length = len(x)
print(x, length)
一个想法,用recursive pattern和PyPI regex module一起使用。在
See this pcre demo at regex101
\b[A-Za-z]+\s+
匹配word boundary,one or morealpha,一个或多个空白(?R)?
递归部分:optionally从头开始粘贴模式\(?
需要使括号成为可选的,以便递归适合\)?
[A-Z](?=[A-Z]*\)
将一个大写字母if followed by结束)
与中间的任何A-Z匹配[A-Z](?=[A-Z]*\))
更改为^{使用regex匹配查找匹配开始的位置。然后使用python字符串索引获取匹配开始之前的子字符串。将子串按单词拆分,得到最后n个单词。其中n是缩写的长度。在
打印:
^{pr2}$这能解决你的问题吗?在
事实上,基廷比我早
相关问题 更多 >
编程相关推荐