python - python中的字符串匹配

标签 python string-matching

我遇到了以下问题。比方说,我在字典的两个列表中有一些字符串:

 left                                right
british                             7
cuneate nucleus                     Medulla oblongata
Motoneurons                         anterior

我在文件中有一些测试行,如下所示:

<s id="69-7">British Meanwhile is the studio 7 album by british pop band 10cc 7.</s>
<s id="5239778-2">Medulla oblongata,the name refers collectively to the cuneate nucleus and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.</s>
<s id="21120-99">Terior horn cells, motoneurons located in the spinal.</s>

我想通过以下方式获得输出:

<s id="69-7"><w2>British</w2> Meanwhile is the studio <w2>7</w2> album by <w1>british</w1> pop band 10cc <w2>7</w2>.</s>
<s id="5239778-2"><w2>Medulla oblongata</w2>,the name refers collectively to the <w1>cuneate nucleus</w1> and gracile nucleus, which are present at the junction between the spinal cord and the <w2>medulla oblongata</w2>.</s>

我尝试了以下代码:

import re

def textReturn(left, right):
    text = ""
    filetext = open(text.xml, "r").read()
    linelist = re.split(u'[\n|\r\n]+',filetext)

    for i in linelist:
        left = left.strip()
        right = right.strip()

        if left in i and right in i:
            i1 = re.sub('(?i)(\s+)(%s)(\s+)'%left, '\\1<w1>\\2</w1>\\3', i)
            i2 = re.sub('(?i)(\s+)(%s)(\s+)'%right, '\\1<w2>\\2</w2>\\3', i1)
            text = text + i2 + "\n"         
    return text   

但它给了我:

'<s id="69-7">British meanwhile is the studio <w2>7</w2> album by <w1>British</w1> pop band 10cc 7.</s>'.
<s id="5239778-2">Medulla oblongata,the name refers collectively to the <w1>cuneate nucleus</w1> and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.</s>
<s id="21120-99">Terior horn cells, <w1>motoneurons</w2> located in the spinal.</s>

即如果开头和结尾有字符串,则不能标记。

此外,我只想返回那些匹配左右字符串的行,而不是其他行。

请提供任何解决方案!非常感谢!!!

最佳答案

它不会在开头和结尾标记,因为您希望关键字前后有一个或多个空格。

使用 \b(分词符)代替 \s+

附录

实际代码:

import re

dict = [('british','7'),('cuneate nucleus','Medulla oblongata'),('Motoneurons','anterior')]

filetext = """<s id="69-7">British Meanwhile is the studio 7 album by british pop band 10cc 7.</s>
<s id="5239778-2">Medulla oblongata,the name refers collectively to the cuneate nucleus and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.</s>
<s id="21120-99">Terior horn cells, motoneurons located in the spinal.</s>
"""

linelist = re.split(u'[\n|\r\n]+', filetext)

s_tag = re.compile(r"(<s[^>]+>)(.*?)(</s>)")

for i in range(3):
    left, right = dict[i]

    line_parts = re.search(s_tag, linelist[i])
    start = line_parts.group(1)
    content = line_parts.group(2)
    end = line_parts.group(3)

    left_match = "(?i)\\b(%s)\\b" % left
    right_match = "(?i)\\b(%s)\\b" % right
    if re.search(left_match, content) and re.search(right_match, content):
        line1 = re.sub(left_match, '<w1>\\1</w1>', content)
        line2 = re.sub(right_match, '<w2>\\1</w2>', line1)
        print(line_parts.group(1) + line2 + line_parts.group(3))

这是短期解决方案的基础,但从长远来看,您应该尝试 XML 解析器方法。

关于python - python中的字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6891365/

相关文章:

python - 从另一个线程关闭挂起的 zmq 套接字

python - 使用 python 和 twisted 聊天 cometd 网站

regex - 在PowerShell中匹配字符串

python - 根据共存规则将列表拆分为多个组

python - 如何根据 ID 和指向 df2 中列名称的值从另一个数据帧 (df2) 中的列获取值。 python / Pandas

python - 字符串索引在 python 中的一个代码位置必须是整数

JavaScript eval() 语法

algorithm - 查找重复位模式的有效算法?

c# - 字符串解析与匹配算法

sql - Elasticsearch,查询字符串,需要匹配字符串