首页 > 解决方案 > python 正则表达式从文本中省略复杂的引用样式

问题描述

我已将文件的内容读入 python,我想摆脱所有遵循相同通用格式的引用:

(Author et al., .............. \nGoogle Scholar) # there could be many '\nGoogle Scholar's within the brackets

简介 朗格汉斯胰岛中的内分泌细胞分泌胰岛素和胰高血糖素以响应葡萄糖扰动以维持葡萄糖稳态。分泌胰岛素的 β 细胞表现出形态、功能和分子变异,这表明它们可能由具有特殊任务和生理反应的亚群组成(Gutierrez 等人,2017Gutierrez GD Gromada J. Sussel L. Heterogeneity of the胰腺 β 细胞。 Front. Genet. 2017; 8: 22Crossref\nPubMed\nScopus (11)\nGoogle Scholar,Roscioni 等人,2016Roscioni SS Migliorini A. Gegg M. Lickert H. 胰岛结构对细胞异质性、可塑性和功能的影响。 Rev. Endocrinol. 2016;12: 695-709Crossref\nPubMed\nScopus (36)\nGoogle Scholar)。β 细胞异质性的特征包括葡萄糖反应性和分泌活性……然而,如果不使用光可切换染料等专业技术,则无法将胰腺中的转录物可视化(Cui et al., 2018Cui Y. Hu D. Markillie LM Chrisler WB Gaffrey MJ Ansong C. Sussel L. Orr G. 基于波动定位成像的荧光原位杂交 (fliFISH),用于准确检测和计数单细胞中的 RNA 拷贝。Nucleic Acids Res. 2018;46:e7Crossref\nPubMed\nScopus ( 2)\n谷歌学术)。我们优化了标准组织 smFISH 协议(Lyubimova 等人,2013Lyubimova A. Itzkovitz S. Junker JP Fan ZP Wu X. van Oudenaarden A. Single-molecule mRNA detection and count in哺乳动物组织.Nat. Protoc. 2013; 8:

期望的输出

简介 朗格汉斯胰岛中的内分泌细胞分泌胰岛素和胰高血糖素以响应葡萄糖扰动以维持葡萄糖稳态。分泌胰岛素的β细胞表现出形态、功能和分子变异,这表明它们可能由具有特殊任务和生理反应的亚群组成。β 细胞异质性的特征包括葡萄糖反应性和分泌活性……然而,如果不使用诸如光开关染料等专门技术,则无法在胰腺中显示转录本。我们通过将探针杂交步骤之前的 mRNA 变性时间从 5 分钟增加到至少 3 小时,优化了标准组织 smFISH 协议。

我找不到一次性省略所有引用的正则表达式,因此我必须分两部分执行此操作:

  1. 查找每个 '\nGoogle Scholar)' 出现的所有位置。
  2. 从每个位置向后延伸,直到出现相应的左括号,然后省略这些索引之间的字符。

我尝试如下:

def remove(test_str):
        regex=re.compile('\\nGoogle Scholar\)')
        starts=[]
        ends=[]
        ret=''
        for end in regex.finditer(test_str): #find all 'Google Scholar)'
            ends.append(m.end())
        for e in ends:                       #find all starting brackets
            i=e
            while True:
                if bool(re.match('\(\D+',test_str[i-2:i])):
                    starts.append(i-2)
                    break
                else:
                    i-=1
        start=test_str[:starts[0]]           #omit all characters in between
        starts=starts[1:]
        end=test_str[ends[-1]:]
        ends=ends[:-1]
        for i,j in zip(starts,ends):
            ret=ret+test_str[j:i]
        return start+ret+end

但是,此策略失败,因为我用来查找每个起始括号 ( \(\D+) 的正则表达式不够准确 - 参考文献中通常有闭合括号,例如

(Cui et al., 2018Cui Y. Hu D. Markillie LM Chrisler WB Gaffrey MJ Ansong C. Sussel L. Orr G. 基于波动定位成像的荧光原位杂交(fliFISH)用于准确检测和计数单细胞中的 RNA 拷贝。Nucleic Acids Res. 2018;46:e7Crossref\nPubMed\nScopus (2)\nGoogle Scholar)

因此,在这种情况下,搜索正确的左括号会过早停止......

任何人都可以推荐一种始终删除所有引用的好方法吗?

标签: pythonregex

解决方案


根据您描述的模式,您可以使用此正则表达式,

(?s)\(.*?Google Scholar\) ?

并将其替换为空字符串。这(?s)是为了启用.匹配新行。

在这里检查

这是一个python代码演示,

import re

s = 'Introduction The endocrine cells in the pancreatic islets of Langerhans secrete insulin and glucagon in response to glucose perturbations to maintain glucose homeostasis. The insulin-secreting beta cells exhibit morphological, functional, and molecular variations, suggesting that they may consist of sub-populations with specialized tasks and physiological responses (Gutierrez etal., 2017Gutierrez G.D. Gromada J. Sussel L. Heterogeneity of the pancreatic beta cell.Front. Genet. 2017; 8: 22Crossref\nPubMed\nScopus (11)\nGoogle Scholar, Roscioni etal., 2016Roscioni S.S. Migliorini A. Gegg M. Lickert H. Impact of islet architecture on -cell heterogeneity, plasticity and function.Nat. Rev. Endocrinol. 2016; 12: 695-709Crossref\nPubMed\nScopus (36)\nGoogle Scholar). Features of beta cell heterogeneity include glucose responsiveness and secretory activity ..... Visualizing transcripts in the pancreas, however, has been infeasible without the use of specialized techniques such as photoswitchable dyes (Cui etal., 2018Cui Y. Hu D. Markillie L.M. Chrisler W.B. Gaffrey M.J. Ansong C. Sussel L. Orr G. Fluctuation localization imaging-based fluorescence insitu hybridization (fliFISH) for accurate detection and counting of RNA copies in single cells.Nucleic Acids Res. 2018; 46: e7Crossref\nPubMed\nScopus (2)\nGoogle Scholar). We have optimized the standard tissue smFISH protocol (Lyubimova etal., 2013Lyubimova A. Itzkovitz S. Junker J.P. Fan Z.P. Wu X. van Oudenaarden A. Single-molecule mRNA detection and counting in mammalian tissue.Nat. Protoc. 2013; 8: 1743-1758Crossref\nPubMed\nScopus (62)\nGoogle Scholar) by substantially increasing the period of mRNA denaturation, which precedes the probe hybridization steps, from 5min to at least 3hr.'

replacedStr = re.sub(r'(?s)\(.*?Google Scholar\) ?','',s)
print(replacedStr)

像您在帖子中提到的那样打印以下内容。

简介 朗格汉斯胰岛中的内分泌细胞响应葡萄糖扰动分泌胰岛素和胰高血糖素以维持葡萄糖稳态。分泌胰岛素的β细胞表现出形态、功能和分子变异,这表明它们可能由具有特殊任务和生理反应的亚群组成。β 细胞异质性的特征包括葡萄糖反应性和分泌活性……然而,如果不使用光可切换染料等专门技术,则无法在胰腺中显示转录本。我们通过将探针杂交步骤之前的 mRNA 变性时间从 5 分钟增加到至少 3 小时,优化了标准组织 smFISH 协议。


推荐阅读