Replace Text in Dozens of HTML Files

J

Jeffrey

I would appreciate suggestions for a general strategy I could use to update
a bunch of HTML files.

Specifically I need to replace sevaral instances of one string with another
string in each of a few dozen HTML files.

While I already plan to use RegEx to do the actual replacement, what should
I be doing to open, update, and save each of the files. What would be an
efficient way to do this? I'm seeing that .NET has a File class, TextWriter,
StreamWriter, StringBuilder, and on and on. There are so many options. Which
ones would work together and what would make sense for my particular
situation?

Thanks.
 
J

Johann Blake

Jeffrey,

Replacing portions of a string is inherent in all classes that are
string oriented (String, StringBuilder, string, etc.). So if replacing
text is your only requirement, any class will do for that. If you need
to append text, then StringBuilder is a better choice.

Using RegEx is a good choice for finding text but you'll have to do
some performance testing to see whether it outperforms a typical
Replace method in the code you are using it in.

Because you are dealing with HTML files, you have one additional
requirement and that is case-sensitivity. To locate and replace HTML
text means that you need to have a method that is case insensitive,
which is what RegEx is good for. On the other hand, there are things in
HTML files that are case-sensitive and need to maintain their case. If
you simply ignore the case while replacing, you may get undesired
side-effects. So be aware of RegEx when replacing case-sensitive text.

I have written applications that replace a large amount of HTML text
and have used StreamReader and StreamWriter because these classes can
be used by many other classes that are derived from them. TextReader
and TextWriter have limited derivatives.

Johann Blake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top