bewise — 2011-07-11T05:15:23-04:00 — #1
Im looking to split a string with HTML tags into two sections. 1st section with a limited number of characters, the next section with the remainder of the original string.
Im currently using the following in order to split the string:
public static string SplitWord(string x, int length)
if (x.Length > length)
x = x.Substring(0, length);
string1 = SplitWord(string, 1000);
string2 = string.Substring(1000);
With my current solution, on the odd occasion it will split up a html tag. For example:
string = "<di"
string1 = "v>";
string = "<div>"
string1 = "</div>";
Is there a way of diving up a string into two parts, but making sure it does after a closing html tag.
string = "<div></div>"
string1 = "";
philiptoop — 2011-07-12T07:01:36-04:00 — #2
I believe what you are after is a regular expression
var strText="<div>when <b>in doubt</b> do nothing</div>";
var strBits = strText.match(/<[^> ]+[^>]*>[^<]*/g);
Will split the string so you end up with an array
strBits = "<div when "
strBits = "<b>in doubt"
strBits = "</b> do nothing"
strBits = "</div>"
endermb — 2011-07-12T07:26:21-04:00 — #3
Never, ever, ever use a regular expression or string manipulation when dealing with HTML. Tasks like this are what HTML parsers like HtmlAgilityPack. were made for!
If you can get to grips with a bit of XPath, then you can separate the content from the HTML, do your necessary splits and then rebuild the HTML around it however you wish.
philiptoop — 2011-07-12T08:00:14-04:00 — #4
endermb — 2011-07-12T08:24:32-04:00 — #5
This thread is in the .NET forum, which is the reason I posted a .NET library, unless I'm missing something?
Regardless, it's a matter of language. HTML is too complex a language to parse with Regular Expressions, as this question on SO shows.
I think the flaw here is that HTML is a Chomsky Type 2 grammar (context free grammar) and RegEx is a Chomsky Type 3 grammar (regular expression). Since a Type 2 grammar is fundamentally more complex than a Type 3 grammar - you can't possibly hope to make this work. But many will try, some will claim success and others will find the fault and totally mess you up.
philiptoop — 2011-07-12T09:14:21-04:00 — #6
This thread is in the .NET forum,
Indeed it is - my mistake.
williamjerry — 2011-07-13T23:40:14-04:00 — #7
I think only regular expression can help in this case otherwise code will get very heavy.
endermb — 2011-07-14T02:34:03-04:00 — #8
Read my post again.
It is linguistic fact that a regular expression is not capable of handling HTML.
honeymonster — 2011-07-14T03:12:35-04:00 — #9
Ahem. Actually with the .NET extensions to regular expressions - specifically the way you can create expressions which matches levels - I would argue that it can be done.
It will not be pretty - and you are correct to point to alternative solutions. I'm just being obnoxious.
endermb — 2011-07-14T04:12:07-04:00 — #10
I think this SO post (despite it relating to JSON) covers my opinion on that method.
"Some systems offer extensions to regular expressions that kinda-sorta handle balanced expressions. However they're all ugly hacks, they're all unportable, and they're all ultimately the wrong tool for the job."
I've used extensions before (admittedly not with .NET) and it was far more trouble than it was worth, and it didn't handle wild HTML code very well. More often than not if you're performing a simple string task on a bit of HTML then the HtmlAgilityPack will do it in a couple of lines. I'd argue that it's the best .NET library I've ever used, and like many developers it brings me great pain to see anyone using a regular expression to parse HTML.