首页 \ 问答 \ 如何删除除img外的所有html标签?(How to remove all html tags except img?)

如何删除除img外的所有html标签?(How to remove all html tags except img?)

我收到了一些html文本,其中包含各种各样的html标签,例如<table>, <a>, <img>等。

现在我想用正则表达式去除除<img ...></img> (和大写<IMG></IMG> )之外的所有html标记。

这个怎么做?


更新:

我的任务非常简单,它只是在头版中打印html的文本内容(包括图像)作为摘要,所以我认为正则表达式很好,很简单。


再次更新

也许一个样本会让我的问题更好理解:)

有一些html文本:

<html>
  <head></head>
  <body>
     Hello, everyone. Here is my photo: <img src="xxx.jpg" />. 
     And, <a href="xxx">know more</a> about me!
  </body>
</html>

我想保留,并删除其他标签。 以下是我想要的:

Hello, everyone. Here is my photo: <img src="xxx.jpg" />. And, know more about me!

现在我编码如下:

html.replaceAll("<.*?>", "")

但它会删除<>之间的所有内容,但我想保留<img xxx></img> ,并删除< and >之间的其他内容

谢谢大家!


I got some html text, which contains all kinds of html tags, such as <table>, <a>, <img>, and so on.

Now I want to use a regular expression to remove all the html tags, except <img ...> and </img>(and upper case <IMG></IMG>).

How to do this?


UPDATE:

My task is very simple, it just print the text content(including images) of a html as a summary in the front page, so I think regular expression is good and simple enough.


UPDATE AGAIN

Maybe a sample will make my question better to understand :)

There are some html text:

<html>
  <head></head>
  <body>
     Hello, everyone. Here is my photo: <img src="xxx.jpg" />. 
     And, <a href="xxx">know more</a> about me!
  </body>
</html>

I want to keep , and remove other tags. Following is what I want:

Hello, everyone. Here is my photo: <img src="xxx.jpg" />. And, know more about me!

Now I code like this:

html.replaceAll("<.*?>", "")

But it will remove all the content between < and >, but I want to keep <img xxx> and </img>, and remove the other content between < and >

Thank for everyone!


原文:https://stackoverflow.com/questions/3297300
更新时间:2023-09-24 12:09

最满意答案

如果你纯粹看绘画的速度,你可以将图片分成许多较小的图片,这些图片会同时处理。 执行此操作的最有效方法是使用多个线程。

如果您需要更多信息,请评论。


If you're looking purely at speed of drawing, you could split the picture into a number of smaller pictures which are processed simultaneously. The most effective way of doing this would be using multiple threads.

Comment if you need more info.

相关问答

更多
  • 你不需要在execute方法中传递一个Thread。 相反,您需要传递一个Runnable对象,如下所示: ExecutorService tasker = Executors.newFixedThreadPool(20); for(double t = 0.0; t<=5.0; t = t + 0.50 ){ // t is time interval tasker.execute(new MyRunnable(t)); } .... .... class MyRunnable implement ...
  • 最大的疑问是内联视图(或派生表,在MySQL版本中).MySQL将实现派生表,然后外部查询将针对它运行。 另一个嫌疑人是SELECT列表中的相关子查询。 这将由外部查询返回的每一行执行。 此外, ORDER BY app_created将需要Using filesort操作(除非您运行的是可能在派生表上构建索引的更新版本的MySQL。) 有一些奇怪的谓词,例如c.deleted = c.deleted 对于已deleted列中具有非NULL值的每一行,情况都是如此。 这相当于c.deleted IS NOT ...
  • 由于nPr有一个明确的公式nPr(n, m) = n!/((nm)!)你一定要尝试使用它。 我的提示是: 记住那个! = n *(n-1)* ... * 2 * 1 请注意, while循环 (是的,循环,而非递归^^)可以极大地优化计算(除法取消了很多因子,让你得到乘法nPr(n, m) = n*(n-1)*...*(n-m+2)*(n-m+1) ) 最后,您应该在计算nPr(n,m) 后计算模数,以避免冗余模运算。 如果它有帮助,你可以尝试制定一个循环不变量 ,这几乎是一个声明,对于n和m所有有效值都应 ...
  • 查看HayStack的大型应用程序,上传到文件系统然后存储到数据库就足以满足数百万的小型应用程序。 但是,像flickr这样的大公司使用干草堆对象。 Look into HayStack for large applications, uploading into a file system then storing into a DB would be sufficient for smaller applications even up to millions. However, larger comp ...
  • 显然,在一个视图中,数千个图像不会一次全部显示,但正如您所描述的那样,在一个长列表中。 我将采取的方法是缓冲图像并在滚动时显示它们。 这将要求您计算可以连续存储的图像数量,并且在当前滚动位置的前后至少缓冲两行或三行。 这种方法的唯一缺点是,在缩略图行很大的情况下获得平滑的滚动解决方案可能会很棘手。 Obviously the thousands of images are not going to be all displayed at once in a single view but as you de ...
  • 如果你纯粹看绘画的速度,你可以将图片分成许多较小的图片,这些图片会同时处理。 执行此操作的最有效方法是使用多个线程。 如果您需要更多信息,请评论。 If you're looking purely at speed of drawing, you could split the picture into a number of smaller pictures which are processed simultaneously. The most effective way of doing this w ...
  • 间隔15分钟: with i as ( select cf.tagindex, min(dateandtime) dateandtime from contfloattable cf group by floor(extract(epoch from dateandtime) / 60 / 15), cf.tagindex ) select cf.dateandtime, cf."Val", cf.status, t.tagname from ...
  • 您可以将每个节点的边缘列表存储在单个阵列中。 如果每个节点的边数是可变的,则可以使用空边终止列表。 这将避免许多小列表(或类似数据结构)的空间开销。 结果可能如下所示: enum { MAX_NODES = 875713, MAX_EDGES = 5105039, }; int nodes[MAX_NODES+1]; // contains index into array edges[]. // index ...
  • 你有几个资源浪费在这里。 您正在检索的JSON文件比您需要的次数多24倍。 将调用移到内部循环中的file_get_contents() 。 准备好的陈述应该与这个念头一起使用:准备一次,执行很多次。 将语句准备移到任何循环之外,然后在循环内部执行它。 你没有建立一个适当的预备陈述。 该语句应该包含占位符( ?或:label格式),但是您要将值插入到您的中。 您传递给execute()所有参数都将被忽略。 编辑:实际上,你在页面上插入每一个值24次,即你为11页中的每一页插入24 * 24行。 建议更改摘要 ...
  • 如果你想尝试不同的压缩,你可以从RLE开始,它适合你的数据 - http://en.wikipedia.org/wiki/Run-length_encoding - 即使在最简单的实现中,它也会非常快速。 相关的http://en.wikipedia.org/wiki/Category:Lossless_compression_algorithms包含更多的链接,如果你想自己推出或找到某人的实现,可以从其他算法开始。 随机评论:“......很多速度损失可能......”不是解决性能问题的方法。 测量并查看 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)