Click here to Skip to main content
15,918,193 members
Home / Discussions / C#
   

C#

 
GeneralGoing Unmanaged doesn't free Memory Pin
Guilio karadanais19-Jul-04 4:24
Guilio karadanais19-Jul-04 4:24 
GeneralRe: Going Unmanaged doesn't free Memory Pin
Heath Stewart19-Jul-04 4:37
protectorHeath Stewart19-Jul-04 4:37 
GeneralRe: Going Unmanaged doesn't free Memory Pin
Guilio karadanais19-Jul-04 5:07
Guilio karadanais19-Jul-04 5:07 
GeneralHex to Dec conversion (big numbas!) Pin
Blubbo19-Jul-04 4:06
Blubbo19-Jul-04 4:06 
GeneralRe: Hex to Dec conversion (big numbas!) Pin
Heath Stewart19-Jul-04 4:30
protectorHeath Stewart19-Jul-04 4:30 
GeneralEAP Certificate requests... Pin
Vodstok19-Jul-04 4:03
Vodstok19-Jul-04 4:03 
GeneralRe: EAP Certificate requests... Pin
Heath Stewart19-Jul-04 4:42
protectorHeath Stewart19-Jul-04 4:42 
GeneralRegular Expressions Pin
Anonymous19-Jul-04 3:54
Anonymous19-Jul-04 3:54 
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:
"<a href="javascript:selectItem('A0-1234',42)">A0-1234</a>"
and "B1-123456" in the html file should get replaced by:
"<a href="javascript:selectItem('B1-123456',42)">B1-123456</a>"

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 = "<a href=\"javascript:selectItem(\'" + cline + "\',42)\">" + cline + "</a>";
*/

if (iline != null)
{
// Replace all matched expressions with the
// appropriate function

Match m = Regex.Match( iline, cline );
String replaceString = "<a href=\"javascript:selectItem(\'" + m.ToString() + "\',42)\">" + m.ToString() + "</a>";
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 :
"<a href="javascript:selectItem('A0-1234',42)">A0-1234</a>"

even if those expressions are "B4-12345", "T6-886745" or anything else that matches "[A-Z,a-z]\d-\d{4,8}"


Please help.....

Questionhow do I get the cursor position?? Pin
Stephan Wright19-Jul-04 1:31
Stephan Wright19-Jul-04 1:31 
AnswerRe: how do I get the cursor position?? Pin
misterbear19-Jul-04 2:07
misterbear19-Jul-04 2:07 
GeneralRe: how do I get the cursor position?? Pin
Stephan Wright19-Jul-04 2:19
Stephan Wright19-Jul-04 2:19 
GeneralRe: how do I get the cursor position?? Pin
Heath Stewart19-Jul-04 3:18
protectorHeath Stewart19-Jul-04 3:18 
GeneralRe: how do I get the cursor position?? Pin
Stephan Wright19-Jul-04 3:53
Stephan Wright19-Jul-04 3:53 
Questionhow to get the supported cryptographic algorithm on current .net platform Pin
noosword18-Jul-04 23:54
noosword18-Jul-04 23:54 
AnswerRe: how to get the supported cryptographic algorithm on current .net platform Pin
Heath Stewart19-Jul-04 2:39
protectorHeath Stewart19-Jul-04 2:39 
GeneralQuestion abt service Pin
SatyaDY18-Jul-04 23:51
SatyaDY18-Jul-04 23:51 
GeneralRe: Question abt service Pin
RB@Emphasys19-Jul-04 6:44
RB@Emphasys19-Jul-04 6:44 
GeneralReferences controls created at runtime Pin
JKtracksub418-Jul-04 21:41
JKtracksub418-Jul-04 21:41 
GeneralRe: References controls created at runtime Pin
Heath Stewart19-Jul-04 2:51
protectorHeath Stewart19-Jul-04 2:51 
GeneralCast Problem Pin
Reinier van de Wetering18-Jul-04 21:28
Reinier van de Wetering18-Jul-04 21:28 
GeneralRe: Cast Problem Pin
mav.northwind18-Jul-04 21:47
mav.northwind18-Jul-04 21:47 
GeneralRe: Cast Problem Pin
Reinier van de Wetering18-Jul-04 22:16
Reinier van de Wetering18-Jul-04 22:16 
GeneralRe: Cast Problem Pin
RB@Emphasys19-Jul-04 6:48
RB@Emphasys19-Jul-04 6:48 
QuestionCrystal Reports: Bad performance on some Win2000 systems? Pin
Pain_Elemental18-Jul-04 18:05
Pain_Elemental18-Jul-04 18:05 
AnswerRe: Crystal Reports: Bad performance on some Win2000 systems? Pin
Daniel Turini19-Jul-04 6:09
Daniel Turini19-Jul-04 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.