开发者

Regexp that matches all the text content of a HTML input

I have articles on my website which I would like to get corrected and translated automatically. But I need to get the content, without having the HTML tags around.

The idea is to have 开发者_运维百科a regex that could retrieve all the content between the tags (and, if possible, also the content found in tags fields like <img alt='Little house'>). The problem is that I don't really know how to write such a regex. Any ideas?


I would recommend using an HTML parser, rather than relying on a regex. Parsing HTML with regex is generally a no-no and are nearly impossible to get right for all cases. There are many questions here on SO that arrive at the same conclusion.

EDIT looks like a couple of us had the same idea... Also, here is a question that discusses more parsers.


Perhaps a regular expression is not the best choice for this job (I will spare you the obligatory tirade).

I would recommend that you look into an HTML parsing library to help you here, something like Html Agility Pack.


As people said, regex is not the most recommended way, but if you decide that regex is the way to go, this should get you started:

string pattern = @"(<(/?[^>]+)>)"
strippedString = Regex.Replace(str, pattern, string.Empty);


Not sure if this helps but I have the ability to translate articles on my site into a readers preferred language, I done this using the Bing translation widget so I don't do any parsing of html it's all done for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜