且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我在做什么毛病我的正则表达式?

更新时间:2023-02-21 12:30:33

要回答没有无用的生命吸取你的问题,你有因为贪婪量词的烦恼。尝试通过添加问号使他们懒惰的:

To answer your question without useless life lessons, you are having troubles because of greedy quantifiers. Try making them lazy by adding question marks:

<meta\\s+?name=\"description\"\\s+?content=\"(?<Description>.*?)\"\\s*?/>

当然这个正则表达式将不会在世界上的所有网页的工作,但如果你只需要尽自己的模板,一些快速更换的脚本,正则表达式是最快和最简单的解决方案,要走的路。

Sure this regex won't work for all pages in the world, but if you need just make some quick replacement script for your own templates, regex is the fastest and easiest solution and the way to go.