如何将这个正则表达式转换成Python

2024-10-01 17:40:30 发布

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

我想在Python中使用这个正则表达式:

 <(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>

(来自RegEx match open tags except XHTML self-contained tags

^{pr2}$

似乎我不能直接将复杂的正则表达式替换为上面的函数。在


Tags: 函数selfmatchtagsopenregexxhtmlexcept
2条回答

在这里工作得很好。你可能因为引用了这些话而遇到麻烦。三重引用:

def removeHtmlTags(page):
    p = re.compile(r'''<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>''')
    return p.sub('', page)

如果需要删除HTML标记,请执行以下操作:

import re

def removeHtmlTags(page):
    pattern = re.compile(r'\<[^>]+\>', re.I)
    return pattern.sub('', page)

相关问题 更多 >

    热门问题