我需要将前面的单词添加到-number中,它发生在句子的-number之前。请查看输入字符串和预期输出字符串,以获得更多的说明。我用静态的方式尝试了正则表达式的.replace,.sub方法,这是一种被操纵的输出。
输入字符串:
acnes刺激角质形成细胞中白细胞介素(IL)-1、-8、LL-37、MMP-1、-2、-3、-9和-13的mRNA表达。
预期输出字符串:
acnes刺激角质形成细胞中白细胞介素(IL)-1、白细胞介素-8 (IL)-8、LL-37、MMP-1、MMP-2、MMP-3、MMP-9和MMP-13的mRNA表达。
代码:
import re
string_a = "The acnes stimulated the mRNA expression of interleukin (IL)-1, -8, LL-37, MMP-1, -2, -3, -9, and -13 in keratinocytes."
regex1 = re.findall(r"[a-z]+\s+\(+[A-Z]+\)+-\d+\,\s+-\d\,+", string_a)
regex2 = re.findall(r"[A-Z]+-\d+\,\s+-\d\,\s+-\d\,\s+-\d\,\s+[a-z]+\s+-\d+", string_a)发布于 2021-03-05 10:27:00
您可以使用
import re
string_a = "The acnes stimulated the mRNA expression of interleukin (IL)-1, -8, LL-37, MMP-1, -2, -3, -9, and -13 in keratinocytes."
pattern = re.compile(r"\b([A-Za-z]+\s*\([A-Z]+\)|[A-Z]+)(\s*-\d+(?:,\s*-\d+)*)(?:,\s*and\s+(-\d+))?")
print( pattern.sub(lambda x: x.group(1) + f', {x.group(1)}'.join(map(str.strip, x.group(2).strip().split(','))) + (f', and {x.group(1)}{x.group(3)}' if x.group(3) else ''), string_a) )
# => The acnes stimulated the mRNA expression of interleukin (IL)-1, interleukin (IL)-8, LL-37, MMP-1, MMP-2, MMP-3, MMP-9, and MMP-13 in keratinocytes.详细信息
\b -字边界([A-Za-z]+\s*\([A-Z]+\)|[A-Z]+)字母,然后是零或多个空格,(,一个或多个大写字母和),或一个或多个大写字母(\s*-\d+(?:,\s*-\d+)*) -捕获组2:零或多个空白空间,-,一个或多个数字,然后零或多个逗号序列,零或多个空白空间,-和一个或多个数字(?:,\s*and\s+(-\d+))? -一个可选的非捕获组:逗号、零或多个空白空间、and、一个或多个空白空间,然后是捕获组3:-,一个或多个数字。Group 1值在lambda中的所有以逗号分隔的数字中占优势,用作替换参数。
如果组3匹配,则追加and+space+concatenated Group 1和Group 3值。
https://stackoverflow.com/questions/66487506
复制相似问题