Hi, I'm trying to replace some text in an html file based on some regular
expressions (or literal text) in a text file. What should happen is that
"A0-1234" in the html file should get replaced by:
"A0-1234"
and "B1-123456" in the html file should get replaced by:
"B1-123456"
and so on... Here's what I'm doing: static void Main(string[] args) { /* For
following code: * i - input; c - config; o - output; * Therefore, isr - input
streamreader, csr - config streamreader, * cline - config line, iline - input
line */ String ifile = "data.html"; String cfile = "config.txt" String ofile = "newdata.html";
// output file name string cline; string iline=""; try { // open a StreamReader
to read in the input file using (StreamReader isr = new StreamReader(ifile)) {
// Read in the entire input file into a single string iline = isr.ReadToEnd();
// open another StreamReader to read in the config file using (StreamReader csr
= new StreamReader(cfile)) { // open a StreamWriter to write out to the output
file using (StreamWriter sw = new StreamWriter(ofile)) { // Read in the config
file, one line at a time. // Each line corresponds to an expression to be
matched // in the input file while ((cline = csr.ReadLine()) != null) { // Set
every line of the config file as the // regular expression to be tested for
Regex testExp = new Regex( cline ); /* // Setup the replacement string //
INSTEAD OF cline, USE MATCHED VALUE string replaceString = ""
+ cline +
""; */ if (iline != null) { // Replace all matched expressions
with the // appropriate function Match m = Regex.Match( iline, cline ); String
replaceString = ""
+ m.ToString() +
""; m = m.NextMatch(); iline = testExp.Replace( iline, replaceString );
Console.WriteLine(iline); }// end if iline } // end while cline // Write the
replaced text back to the output file // using the StreamWriter sw.Write(iline);
} // end using streamwriter sw } // end using StreamReader csr } // end using
StreamReader isr } // End Try block catch (Exception e) { Console.WriteLine("The
file could not be read:"); Console.WriteLine(e.Message); } // End catch block }
// End Main Now the problem is that ALL matches of a regular expression in the
config file (say "[A-Z,a-z]\d-\d{4,8}", which matches "A0-1234", "B6-12345678",
"D4-12345" etc) get replaced by the first match... So if the first expr matched
in the input html file is "A0-1234", then all other expressions in the html file
get replaced by : "A0-1234"
even if those expressions are "B4-12345", "T6-886745" or anything else that
matches "[A-Z,a-z]\d-\d{4,8}" Please help.....