何时使用正则表达式与HTML是明智的? [英] When is it wise to use regular expressions with HTML?

查看:110
本文介绍了何时使用正则表达式与HTML是明智的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管regexp并不是完全解析HTML文档的正确工具,但我发现许多人盲目地忽视了有关正则表达式的任何问题,只要它们与在提议的文本中看到单个HTML标记一样多。

由于我们看到很多regexp not 作为正确工具的例子,所以我想问一下你的看法:简单模式匹配是比使用完整解析引擎更好的解决方案吗?解析方案

如果您正在寻找解析的HTML集已知正则表达式符合某种模式。例如如果您知道没有注释掉的HTML或复杂的场景等。

例如。我经常鼓吹你不应该使用HTML的正则表达式,但是如果我有一套我熟悉的HTML,很简单,并且我可以很容易地检查后操作,那么我对使用正则表达式没有任何疑问那是。


While it's absolutely true that regexp are not the right tool to fully parse HTML documents, I am seeing a lot of people blindly disregarding any question about regexp if they as much as see a single HTML tag in the proposed text.

Since we see a lot of examples of regexp not being the right tool, I ask your opinion on this: what are the cases where a simple pattern match is a better solution than using a full parsing engine?

解决方案

If the set of HTML you're looking to parse with a regexp is known to conform to some sort of pattern. e.g. if you know there's no commented-out HTML, or complex scenarios etc.

e.g. I often preach that you shouldn't use regexps for HTML, but if I have a set of HTML that I'm familiar with, is straightforward and that I can check easily post-manipulation, then I have no qualms about using a regexp for that.

这篇关于何时使用正则表达式与HTML是明智的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆