从HTML正文提取文本片段(.NET中) [英] Extracting text fragment from a HTML body (in .NET)

查看:351
本文介绍了从HTML正文提取文本片段(.NET中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由用户通过的文本编辑器中输入,因此它可以是几乎任何东西(那些少不应该是body标签外,没有关于头或忧虑的doctype等)的HTML内容。
此内容的一个例子:

 < H1>头1 LT; / H1>
< P>此处的一些文字< / P>< P>此处有些比较文字< / P>
< D​​IV ALIGN =右GT;< A HREF =X>将链接此处< / A>< / DIV><小时/>
< H1>头2'; / H1>
< P>此处的一些文字< / P>< P>此处有些比较文字< / P>
< D​​IV ALIGN =右GT;< A HREF =X>将链接此处< / A>< / DIV><小时/>

诀窍是,我需要只提取文本的第一个100个字符(HTML标签剥离)。我还需要保留换行符,而不是打破任何字。

所以输出上面会是这样的:


 标题1
这里的一些文字一些文字在这里这里的一个链接标题2
这里的一些文字一些


它有98个字符,换行符将被保留。我目前可以做到的,是剥去使用正则表达式所有的HTML标签:

  Regex.Replace(htmlStr,< [^>] *>中,)

然后使用正则表达式以及与修剪长度:

  Regex.Match(textStr,@^ {1100} \\ B)。价值

我的问题是,如何保留换行符?我得到这样的输出:


 标题1
有些文字hereSome更多的文字在这里
这里的一个链接
标题2
有些文字hereSome更多的文本


注意联句?也许有人可以告诉我解决这个问题的一些其他方式。谢谢!

其他信息:我的目的是从一堆HTML内容的生成纯文本大纲。猜猜这将有助于澄清这个问题。


解决方案

好了,我需要关闭这虽然不具有理想的解决方案。由于我的应用程序使用的HTML标签是很常见的(无表,名单等)很少或根本没有嵌套,我所做的是preformat的HTML片段之前,我的用户输入后保存。


  • 删除所有换行符

  • 添加一个换行符preFIX所有块标签(例如DIV,P,HR,H1 / 2/3/4等)

在我解压出来显示为纯文本,使用正则表达式来删除HTML标记并保留换行。几乎没有火箭科学,但对我的作品。

I have an HTML content which is entered by user via a richtext editor so it can be almost anything (less those not supposed to be outside the body tag, no worries about "head" or doctype etc). An example of this content:

<h1>Header 1</h1>
<p>Some text here</p><p>Some more text here</p>
<div align=right><a href="x">A link here</a></div><hr />
<h1>Header 2</h1>
<p>Some text here</p><p>Some more text here</p>
<div align=right><a href="x">A link here</a></div><hr />

The trick is, I need to extract first 100 characters of the text only (HTML tags stripped). I also need to retain the line breaks and not break any word.

So the output for the above will be something like:

Header 1
Some text here

Some more text here

A link here

Header 2
Some text here

Some

It has 98 characters and line breaks are retained. What I can achieve so far is to strip the all HTML tags using Regex:

Regex.Replace(htmlStr, "<[^>]*>", "")

Then trim the length using Regex as well with:

Regex.Match(textStr, @"^.{1,100}\b").Value

My problem is, how to retaining the line break?. I get an output like:

Header 1
Some text hereSome more text here
A link here
Header 2
Some text hereSome more text

Notice the joining sentences? Perhaps someone can show me some other ways of solving this problem. Thanks!

Additional Info: My purpose is to generate plain text synopsis from a bunch of HTML content. Guess this will help clarify the this problem.

解决方案

Well, I need to close this though not having the ideal solution. Since the HTML tags used in my app are very common ones (no tables, list etc) with little or no nesting, what I did is to preformat the HTML fragments before I save them after user input.

  • Remove all line breaks
  • Add a line break prefix to all block tags (e.g. div, p, hr, h1/2/3/4 etc)

Before I extract them out to be displayed as plain-text, use regex to remove the html tag and retain the line-break. Hardly any rocket science but works for me.

这篇关于从HTML正文提取文本片段(.NET中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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