正则表达式 Python 删除样式标签之间的内容(包含\n\r)
我对正则表达式有点困惑。我有一段文本,需要去掉标签里面的样式。对于像这样一行的文本,我用这个正则表达式<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>
3 个回答
0
使用这个正则表达式,'.' 这个符号不包括换行符 \n。
<style>((?:.|\n)*)<\/style>
0
2
使用 s 修饰符。
(?s) 表示“单行模式”,这样点号(.)就可以匹配所有字符,包括换行符。这个功能在 Ruby 和 JavaScript 中不支持。在 Tcl 语言中,(?s) 还会让开头的 ^ 和结尾的 $ 只匹配字符串的开始和结束。
或者可以使用:
<style>([\s\S]*)<\/style>