我们如何在 Perl 正则表达式中匹配任何单个字符(包括换行符)?
我想使用 UltraEdit 正则表达式 (perl) 将以下文本替换为一堆 html 文件中的其他文本:
<style type="text/css">
#some-id{}
.some-class{}
//many other css styles follow
</style>
我尝试使用
正则表达式应该是什么样的?
非常感谢大家。
I would like to use UltraEdit regular expression (perl) to replace the following text with some other text in a bunch of html files:
<style type="text/css">
#some-id{}
.some-class{}
//many other css styles follow
</style>
I tried to use <style type="text/css">.*</style>
but of course it wouldn't match anything because the dot matches any character except line feed. I would like to match line feed as well and the line feed maybe either \r\n
or \n
.
How should the regular expression look like?
Many thanks to you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常点
.
匹配除换行符之外的所有字符。使用 s 正则表达式中的修饰符强制点匹配所有字符,包括换行符。Normally dot
.
matches all characters other than new line. Use thes
modifier in the regex to force dot to match all characters including new line.在 UltraEdit 中,您需要在正则表达式中添加
(?s)
以使点与换行符匹配。即,搜索
I've also made the quantifier lazy (
.*?
) 因为否则您将匹配从第一个到最后一个的所有内容整个文件中的
。
另请注意,这是一个不稳定的解决方案,因为正则表达式根本无法可靠地解析 HTML。在 UltraEdit 中,这就是您所拥有的一切 - 脚本语言和解析器会更好,但如果它适用于您的情况,那就太好了。只需确保匹配的内容不会多于(或少于)您想要的内容(例如
//comment contains a tag
)。In UltraEdit, you need to prepend
(?s)
to your regex to make the dot match newline.I. e., search for
I've also made the quantifier lazy (
.*?
) because otherwise you would match everything from the first<style>
to the last</style>
in your entire file.Also be advised that this is a flaky solution because regular expressions can't parse HTML reliably if at all. In UltraEdit, that's all you have - a scripting language and a parser would be better, but if it works in your case, then great. Just make sure you don't match more (or less) than you wanted (think
//comment containing a </style> tag
).