【问题标题】:I need a regex to find a phrase between two tags python [duplicate]我需要一个正则表达式来在两个标签 python [重复] 之间找到一个短语
【发布时间】:2020-10-11 23:57:09
【问题描述】:

我有两个标签告诉我短语是什么类型,我需要在短语中找到它们并从它们中提取短语以及它们的位置

短语示例: "您好,我的名字是 TYPE_PER María Pérez END_TYPE,我来自 TYPE_LOC Europe END_TYPE"

我希望你提取短语“María Pérez” TYPE_PER 和 END_TYPE 之间的内容

【问题讨论】:

标签: python regex


【解决方案1】:

.splite() 使用

text = "Hello, my name is-María Pérez-and I'm from-Europe-"

test = text.split('-')

for i in range(int(len(test)/2)):
    print(test[i*2+1])

【讨论】:

    【解决方案2】:

    我会给你一个更好的,找到所有的TYPE_*标签和它们的值:

    >>> import re
    >>> s = "Hello, my name is TYPE_PER María Pérez END_TYPE and I'm from TYPE_LOC Europe END_TYPE"
    >>> dict(re.findall("(TYPE_[A-Z]+)\s*(.+?)\s*END_TYPE", s))
    {'TYPE_PER': 'María Pérez', 'TYPE_LOC': 'Europe'}
    

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多