【问题标题】:replace Regex expression with the match [duplicate]用匹配替换正则表达式[重复]
【发布时间】:2019-10-19 18:56:47
【问题描述】:

我有以下字符串:message = 'hi <@ABC> and <@DEF>', 以及以下正则表达式:exp = '<@(.*?)>',因此re.findall(exp, message) 输出['ABC', 'DEF']。如何用这些输出替换原始消息匹配,以便得到'hi ABC and DEF'

【问题讨论】:

  • 替换为r'\1'

标签: python regex


【解决方案1】:
import re

line = re.sub(
           r"<@(.*?)>", 
           r"\1", 
           line
       )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2015-11-30
    • 1970-01-01
    • 2013-12-06
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多