1

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?

dtb
  • 213,145
  • 36
  • 401
  • 431
TigrouMeow
  • 3,669
  • 3
  • 27
  • 31

4 Answers4

2

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.

Community
  • 1
  • 1
jheddings
  • 26,717
  • 8
  • 52
  • 65
1

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.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
1

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);
Elad
  • 19,079
  • 18
  • 62
  • 71
0

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.