我对正则表达式有点困惑,我得到了这个文本,它需要删除标签内的样式。如果它与此正则表达式 <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/