如何删除两个单词之间的字符串

编程入门 行业动态 更新时间:2024-10-24 22:26:44
本文介绍了如何删除两个单词之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用以下代码行下载网页,

I am downloading web pages using below lines of code,

WebRequest request = WebRequest.Create(strURL); WebResponse response = request.GetResponse(); Stream data = response.GetResponseStream(); string html = String.Empty; using (StreamReader sr = new StreamReader(data)) { html = sr.ReadToEnd(); }

然后从这里提取身体部位,如下所示:

then from here I am extracting body part as below:

int nBodyStart = downloadString.IndexOf("<body"); int nBodyEnd = downloadString.LastIndexOf("</body>"); String strBody = downloadString.Substring(nBodyStart, (nBodyEnd - nBodyStart + 7));

现在我要删除正文部分附带的所有javascript,我该怎么做?

Now I want to remove any javascript attached in the body part, How can I do that?

我的目标是获取网页的唯一内容.但是由于每个页面的处理方式可能不同,所以我尝试删除所有js标签,然后使用下面的RegEx删除所有HTML标签

My aim to get the only contents of the web page. But as each page may have different approach, so I am trying to remove any js tags and then remove any HTML tags using below RegEx

Regex.Replace(strBody, @"<[^>]+>|&nbsp;", "").Trim();

但是我不知道如何删除脚本标记之间的js,因为脚本可能是多行或单行.

But I don't know how to remove js between script tags as the script may be multi-line or single line.

谢谢.

推荐答案

要匹配脚本标签(包括标签对的内部),请使用以下命令:

To match script tags (including the inside of the pair), use the following:

<script[^>]*>(.*?)</script>

要匹配所有HTML标记(但不能匹配该对的内部标记),您可以使用:

To match all HTML tags (but not the inside of the pair) you can use:

</?[a-z][a-z0-9]*[^<>]*>

我刚刚意识到您可能也想删除样式标签:

I just realised you might also want to remove style tags too:

<style[^>]*>(.*?)</style>

完整的正则表达式字符串在这里:

Full regular expression string here:

<script[^>]*>(.*?)</script>|<style[^>]*>(.*?)</style>|</?[a-z][a-z0-9]*[^<>]*>|<[^>]+>|&nbsp;

更多推荐

如何删除两个单词之间的字符串

本文发布于:2023-11-02 17:46:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   单词   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!