Re: regex - better way?
From: Dennis Myrén (dennis[DELETETHIS)
Date: 10/29/04
- Next message: Tim Bücker: "this.MaximizeBox (enable/ disable)"
- Previous message: Daniel O'Connell [C# MVP]: "Re: regex - better way?"
- In reply to: rjb: "regex - better way?"
- Next in thread: rjb: "Re: regex - better way?"
- Reply: rjb: "Re: regex - better way?"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 29 Oct 2004 12:16:03 +0200
I would recommend not using regular expression,
but rather load all keys and values from the file into a hashtable
and work with that. It is a very smooth way. This is a code sample
how you would do it:
<code>
// Load the file (filename).
StreamReader sr = new StreamReader(filename);
Hashtable infoTable = new Hashtable();
string [] kvPair = null;
string line = null;
while (null != (line = sr.ReadLine()))
{
kvPair = line.Split('=');
infoTable.Add(kvPair [0].Trim(), kvPair [1].Trim());
}
sr.Close();
// Print all keys and values.
IDictionaryEnumerator de = infoTable.GetEnumerator();
while (de.MoveNext())
{
Console.WriteLine("{0} = {1}", de.Key, de.Value);
}
// Print the user name.
Console.WriteLine("The user is {0}.", infoTable
["User-Name"].ToString());
</code>
You might want to check that kvPair really has two elements, before putting
it into the table, and also handle eventual exceptions thrown when you try
to open the file.
-- Regards, Dennis JD Myrén Oslo Kodebureau "rjb" <RJB@no_spam_VP.PL> wrote in message news:clt3v6$8jl$1@news.onet.pl... > Hi! > > Could somebody have a look and help me to optimize the code below. > It may look like very bad way of coding, but this stuff is very, very new > for me. > > I've included just few lines. > > Regex regxUserName = new Regex(@"(?<=User-Name = )\""([^\""]+)\""", > RegexOptions.None); > Regex regxSessionId = new Regex(@"(?<=Acct-Multi-Session-Id > = )\""([^\""]+)\""", RegexOptions.None); > Regex regxInputGigawords = new Regex(@"(?<=Acct-Input-Gigawords = )\w*", > RegexOptions.None); > . > . > . > > Match mt = regxUserName.Match(sb.ToString()); > strUserName = mt.Groups[1].ToString(); > Match mt2 = regxSessionId.Match(sb.ToString()); > strSessionId = mt2.Groups[1].ToString(); > Match mt3 = regxInputGigawords.Match(sb.ToString()); > strInputGigawords = mt3.Groups[0].ToString(); > . > . > . > > I'm using this to extract data from the following file. > > Mon Sep 27 22:17:15 2004 > Acct-Status-Type = Interim-Update > User-Name = "0007933B22B9" > NAS-IP-Address = 192.168.10.40 > Service-Type = DATA > Acct-Multi-Session-Id = "147738" > Acct-Session-Id = "3" > Acct-Delay-Time = 0 > Event-Timestamp = 1096323434 > Acct-Session-Time = 153766 > Acct-Input-Gigawords = 0 > Acct-Output-Gigawords = 0 > Acct-Input-Octets = 17970689 > Acct-Output-Octets = 8331353 > Acct-Terminate-Cause = 0 > Framed-IP-Address = 0.0.0.0 > Acct-Input-Packets = 0 > Acct-Output-Packets = 0 > NAS-Port-Type = Async > NAS-Port-Id = 0 > > > thank you > rjb > >
- Next message: Tim Bücker: "this.MaximizeBox (enable/ disable)"
- Previous message: Daniel O'Connell [C# MVP]: "Re: regex - better way?"
- In reply to: rjb: "regex - better way?"
- Next in thread: rjb: "Re: regex - better way?"
- Reply: rjb: "Re: regex - better way?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|