是否可以匹配字符串和最近的括号?

2024-06-26 10:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在分析一些引文模式,每个句子都有字符串,通常是这样的

Various chemogenomics [<xref ref-type="bibr" rid="pone.0204999.ref008">8</xref>] methods have been proposed in the last decade [<xref ref-type="bibr" rid="pone.0204999.ref010">10</xref>–<xref ref-type="bibr" rid="pone.0204999.ref024">24</xref>] and also this one [<xref ref-type="bibr" rid="pone.0204999.ref008">8</xref>].

当我试图在一个句子中找到所有的引文时,我找到了

>10<\/xref>–<xref ref-type="bibr" rid="pone.0204999.ref024">24<\/xref>

理想情况下,我能够用方括号(即字符串)识别(并替换)完整的引用

[<xref ref-type="bibr" rid="pone.0204999.ref010">10</xref>–<xref ref-type="bibr" rid="pone.0204999.ref024">24</xref>]

只知道我能匹配的那根弦。我想知道是否有可能采取匹配字符串,并包括所有文本到最近的方括号?你知道吗

我可以在匹配字符串后使用lazy.*?]regex术语匹配右方括号,但不能对左括号执行相同的操作,因为它将找到句子的第一个左括号,而不是最接近匹配字符串的左括号。你知道吗


Tags: 字符串reftype模式句子括号variousrid