Re: regex - better way?

From: Dennis Myrén (dennis[DELETETHIS)
Date: 10/29/04


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
>
> 


Relevant Pages

  • Re: Explorer keep launching at start up
    ... keys in two replys. ... "Load at next startup only"=dword:00000010 ... "Image API Enabled Filters"="BMP GIF JPEG PCX PNG TIFF FPX MIX" ... @="Microsoft Office Environment" ...
    (microsoft.public.windowsxp.general)
  • Re: Naive threading performance questions
    ... The input file line is of the form: ... The keys and values are all ... ASCII in this particular load (which is not true of all my data ... would show up in the dprofpp. ...
    (comp.lang.perl.misc)
  • Re: FC2 wireless woes
    ... > How, specifically, did you load it? ... even though the modules load and the wireless card is detected ... NETDEV WATCHDOG: wlan0: transmit timed out ... Set genstr or keys, not both. ...
    (alt.os.linux.redhat)
  • Re: Changing ComputerName
    ... If you can manually run regedit.exe you can 'load a hive' file to ... good for experimenting before automating. ... A Previous post from Andy Allredincluded: ... >> You could use a script to alter the ComputerName & Hostname keys such as ...
    (microsoft.public.windowsxp.embedded)