python - 正则表达式 Python 在样式标签之间删除(包括\n\r)

标签 python css regex

我对正则表达式有点困惑,我得到了这个文本,它需要删除标签内的样式。如果它与此正则表达式 <style>(.*)<\/style> 像这样在一行中,我就可以正常工作, 很简单:

<style>@page { size: 8.5in 11in; margin-right: 1in; margin-top: 0.5in; margin-bottom: 0.5in }</style>

但是我坚持使用 \r\n对于这篇文章,我无法打败它。

<style>
    @page { size: 8.5in 11in; margin-right: 1in; margin-top: 0.5in; margin-bottom: 0.5in }
    p { margin-bottom: 0.17in; direction: ltr; color: #000000; widows: 2; orphans: 2 }
    p.western { font-family: "Times New Roman", serif; font-size: 12pt; so-language: en-US; font-style: italic; font-weight: bold }
    p.cjk { font-family: "Times New Roman", serif; font-size: 12pt; font-style: italic; font-weight: bold }
    p.ctl { font-family: "Times New Roman", serif; font-size: 10pt; so-language: ar-SA }
    h3 { margin-top: 0in; margin-bottom: 0.17in; direction: ltr; color: #000000; text-align: justify; widows: 2; orphans: 2; page-break-after: auto }
    h3.western { font-family: "Times New Roman", serif; font-size: 12pt; so-language: en-US; font-weight: normal }
    h3.cjk { font-family: "Times New Roman", serif; font-size: 12pt; font-weight: normal }
    h3.ctl { font-family: "Times New Roman", serif; font-size: 10pt; so-language: ar-SA; font-weight: normal }
    a:link { color: #0000ff }
    a:visited { color: #800080 }
    a.western:visited { so-language: en-US }
    a.cjk:visited { so-language: zh-CN }
    a.ctl:visited { so-language: hi-IN }
    a.sdfootnotesym-western { font-size: 8pt }
    a.sdfootnotesym-cjk { font-size: 8pt }
</style>

最佳答案

使用 s 修饰符。

(?s) for "single line mode" makes the dot match all characters, including line breaks. Not supported by Ruby or JavaScript. In Tcl, (?s) also makes the caret and dollar match at the start and end of the string only.

或者使用:

<style>([\s\S]*)<\/style>

关于python - 正则表达式 Python 在样式标签之间删除(包括\n\r),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25077716/

相关文章:

python - 你将如何使用 Django 制作类似 Excel 的用户界面?

python - 进入全屏时屏幕尺寸不更新

html - Wordpress 标题很长,字母之间没有空格

regex - 开始,尝试循环日期正则表达式结果

php - 只删除括号内的数字(方括号)

python - 评估列中唯一值的 True 百分比

python - Django 1.8 操作错误 : no such column:

css - 如何使用关键帧上下移动图像

css - 如何让部分装饰文字 - 不装饰?

javascript - 正则表达式匹配 3 或 6 类型