我们如何在 Perl 正则表达式中匹配任何单个字符(包括换行符)?

发布于 2024-09-02 14:12:48 字数 389 浏览 5 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-09-09 14:12:49

通常点 . 匹配除换行符之外的所有字符。使用 s 正则表达式中的修饰符强制点匹配所有字符,包括换行符。

Normally dot . matches all characters other than new line. Use the s modifier in the regex to force dot to match all characters including new line.

背叛残局 2024-09-09 14:12:49

在 UltraEdit 中,您需要在正则表达式中添加 (?s) 以使点与换行符匹配。

即,搜索

(?s)<style type="text/css">.*?</style>

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

(?s)<style type="text/css">.*?</style>

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文