473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

regex - better way?

rjb
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
Nov 16 '05 #1
8 1828

"rjb" <RJB@no_spam_VP.PL> wrote in message news:cl**********@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.


Are you having any performance issues with this? If you aren't then the
easiest and most maintainable solution is fine(regex is easier to grasp,
once you know regex, than string manipulations and *way* easier to maintain
than a generated parser).

Nov 16 '05 #2
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:cl**********@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

Nov 16 '05 #3
rjb
Thank you for your response.

Daniel - I don't have any issue with performance. I just thought that this
looks "bad".
My experience with regular expresion = couple of hours. Before then I didn't
know such
a thing exists :) I'm not a programmer...

Dennis - thank you for your code. I'm very keen on learning new techniques.

To give you the whole picture. My file looks like:

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

Mon Sep 27 22:17:35 2004
Acct-Status-Type = Interim-Update
User-Name = "00079326AAC8"
NAS-IP-Address = 192.168.10.40
Service-Type = DATA
Acct-Multi-Session-Id = "147817"
Acct-Session-Id = "3"
Acct-Delay-Time = 0
Event-Timestamp = 1096323454
Acct-Session-Time = 900
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Octets = 130612
Acct-Output-Octets = 2058421
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

Mon Sep 27 22:32:34 2004
Acct-Status-Type = Interim-Update
User-Name = "00079330410A"
NAS-IP-Address = 192.168.10.40
Service-Type = DATA
Acct-Multi-Session-Id = "147813"
Acct-Session-Id = "3"
Acct-Delay-Time = 0
Event-Timestamp = 1096324353
Acct-Session-Time = 19429
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Octets = 4137490
Acct-Output-Octets = 11070040
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

.....and so on. A lot of groups.

Basically what I want is to have the output below from each group:

0007933B22B9 27 Sep 2004 147738 0 0 17970689 8331353
00079326AAC8 27 Sep 2004 147817 0 0 130612 2058421
00079330410A 27 Sep 2004 147813 0 0 4137490 11070040
etc...

All this will go to a database.

Thank you for your time.
rjb
"Dennis Myrén" <dennis[DELETETHIS]@oslokb.no> wrote in message
news:BN*******************@news4.e.nsc.no...
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:cl**********@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


Nov 16 '05 #4
Well, then there is some more work.
But it is still very doable using only StreamReader and string.Split.
If you know the file will never be huge, you could just
call ReadToEnd on the StreamReader and perform a split on that string,
splitting on new lines ('\n'), and then work with that array, because it
will be easier when you are not bound to forward-only processing of the
data.

I would suggest you define a class that represent each group to get a little
of structure, like:

public sealed
class Group
{

private Group ( )
{
}

DateTime _timeStamp = null;
Hashtable _table = null;

public DateTime TimeStamp
{
get
{
return _timeStamp;
}
}

public Hashtable DataTable
{
get
{
return _table;
}
}
}
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"rjb" <RJB@no_spam_VP.PL> wrote in message news:cl**********@news.onet.pl...
Thank you for your response.

Daniel - I don't have any issue with performance. I just thought that
this
looks "bad".
My experience with regular expresion = couple of hours. Before then I
didn't
know such
a thing exists :) I'm not a programmer...

Dennis - thank you for your code. I'm very keen on learning new
techniques.

To give you the whole picture. My file looks like:

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

Mon Sep 27 22:17:35 2004
Acct-Status-Type = Interim-Update
User-Name = "00079326AAC8"
NAS-IP-Address = 192.168.10.40
Service-Type = DATA
Acct-Multi-Session-Id = "147817"
Acct-Session-Id = "3"
Acct-Delay-Time = 0
Event-Timestamp = 1096323454
Acct-Session-Time = 900
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Octets = 130612
Acct-Output-Octets = 2058421
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

Mon Sep 27 22:32:34 2004
Acct-Status-Type = Interim-Update
User-Name = "00079330410A"
NAS-IP-Address = 192.168.10.40
Service-Type = DATA
Acct-Multi-Session-Id = "147813"
Acct-Session-Id = "3"
Acct-Delay-Time = 0
Event-Timestamp = 1096324353
Acct-Session-Time = 19429
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Octets = 4137490
Acct-Output-Octets = 11070040
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

....and so on. A lot of groups.

Basically what I want is to have the output below from each group:

0007933B22B9 27 Sep 2004 147738 0 0 17970689 8331353
00079326AAC8 27 Sep 2004 147817 0 0 130612 2058421
00079330410A 27 Sep 2004 147813 0 0 4137490 11070040
etc...

All this will go to a database.

Thank you for your time.
rjb
"Dennis Myrén" <dennis[DELETETHIS]@oslokb.no> wrote in message
news:BN*******************@news4.e.nsc.no...
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:cl**********@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
>
>



Nov 16 '05 #5
Hello RJB,

I agree with Dennis' conclusions. I find simple parsing FAR easier to use,
understand, and debug, than regular expressions.
This is especially true since your data repeats in the data file.

I don't agree with Dennis that you need to read the entire document into
memory, though. I've seen data documents like this, and they can be quite
large. Simply detecting the blank line and the date is sufficient to
seperate groups and do a little processing.

Below, I've taken Dennis' code and added some logic... (warning: uncompiled
code)

// initialize your database object
SqlConnection myConnect = new SqlConnection (Your Connection String);
myConnect.Open();

// Load the file (filename).
StreamReader sr = new StreamReader(filename);
Hashtable infoTable = new Hashtable();
string [] kvPair = null;
string line = null;
while (null != (line = sr.ReadLine()))
{
if (line.Trim.Len = 0) // you've hit a blank line... group ends
{
string Sql_String = string.Format("Insert MyTable (date,
username, inputoctets, outputoctects) values ('{0}', '{1}', '{2}', '{3}')",
infoTable["Date"] , infoTable["User-Name"],
infoTable["Input-Octets"], infoTable["Output-Octets"]);
SqlCommand myCommand = new SqlCommand(Sql_String, myConnect);
myCommand.ExecuteNonQuery();
infoTable.Clear();

}
else
{
kvPair = line.Split('=');
if (kvPair.Length = 1) // this is the date!
{
infoTable.Add("Date",line.Trim());
}
else
{
infoTable.Add(kvPair [0].Trim(), kvPair [1].Trim());
}
}
}
sr.Close();
myConnect.Close();

Assumptions: there's a blank line at the end of the file.
There is no blank line at the beginning of the file.

This code was not compiled... please forgive any syntax errors. I'm typing
from memory.

--- Nick

"rjb" <RJB@no_spam_VP.PL> wrote in message news:cl**********@news.onet.pl...
Thank you for your response.

Daniel - I don't have any issue with performance. I just thought that this looks "bad".
My experience with regular expresion = couple of hours. Before then I didn't know such
a thing exists :) I'm not a programmer...

Dennis - thank you for your code. I'm very keen on learning new techniques.
To give you the whole picture. My file looks like:

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

Mon Sep 27 22:17:35 2004
Acct-Status-Type = Interim-Update
User-Name = "00079326AAC8"
NAS-IP-Address = 192.168.10.40
Service-Type = DATA
Acct-Multi-Session-Id = "147817"
Acct-Session-Id = "3"
Acct-Delay-Time = 0
Event-Timestamp = 1096323454
Acct-Session-Time = 900
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Octets = 130612
Acct-Output-Octets = 2058421
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

Mon Sep 27 22:32:34 2004
Acct-Status-Type = Interim-Update
User-Name = "00079330410A"
NAS-IP-Address = 192.168.10.40
Service-Type = DATA
Acct-Multi-Session-Id = "147813"
Acct-Session-Id = "3"
Acct-Delay-Time = 0
Event-Timestamp = 1096324353
Acct-Session-Time = 19429
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Octets = 4137490
Acct-Output-Octets = 11070040
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

....and so on. A lot of groups.

Basically what I want is to have the output below from each group:

0007933B22B9 27 Sep 2004 147738 0 0 17970689 8331353
00079326AAC8 27 Sep 2004 147817 0 0 130612 2058421
00079330410A 27 Sep 2004 147813 0 0 4137490 11070040
etc...

All this will go to a database.

Thank you for your time.
rjb
"Dennis Myrén" <dennis[DELETETHIS]@oslokb.no> wrote in message
news:BN*******************@news4.e.nsc.no...
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:cl**********@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



Nov 16 '05 #6

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:Hqsgd.21231$HA.7002@attbi_s01...
Hello RJB,

I agree with Dennis' conclusions. I find simple parsing FAR easier to
use,
understand, and debug, than regular expressions.
This is especially true since your data repeats in the data file.

I don't agree with Dennis that you need to read the entire document into
memory, though. I've seen data documents like this, and they can be quite
large. Simply detecting the blank line and the date is sufficient to
seperate groups and do a little processing.

Below, I've taken Dennis' code and added some logic... (warning:
uncompiled
code)

// initialize your database object
SqlConnection myConnect = new SqlConnection (Your Connection String);
myConnect.Open();

// Load the file (filename).
StreamReader sr = new StreamReader(filename);
Hashtable infoTable = new Hashtable();
string [] kvPair = null;
string line = null;
while (null != (line = sr.ReadLine()))
{
if (line.Trim.Len = 0) // you've hit a blank line... group ends
{
string Sql_String = string.Format("Insert MyTable (date,
username, inputoctets, outputoctects) values ('{0}', '{1}', '{2}',
'{3}')",
infoTable["Date"] , infoTable["User-Name"],
infoTable["Input-Octets"], infoTable["Output-Octets"]);
SqlCommand myCommand = new SqlCommand(Sql_String, myConnect);
myCommand.ExecuteNonQuery();
infoTable.Clear();

}
else
{
kvPair = line.Split('=');
if (kvPair.Length = 1) // this is the date!
{
infoTable.Add("Date",line.Trim());
}
else
{
infoTable.Add(kvPair [0].Trim(), kvPair [1].Trim());
}


As a note. If you want to remove quotes you'll have to process that here.
This particular algorithm will result in quoted strings being added to your
DB.
kvPair[1].Trim().Trim('"'); would be sufficent, if mildly messy
Nov 16 '05 #7

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:u8**************@TK2MSFTNGP10.phx.gbl...
<<clipped code block>>
As a note. If you want to remove quotes you'll have to process that here.
This particular algorithm will result in quoted strings being added to your DB.
kvPair[1].Trim().Trim('"'); would be sufficent, if mildly messy


Good point. I missed that detail.

now, if I just had Edit and Continue...
(just kidding :-)

--- Nick
Nov 16 '05 #8

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:XeQgd.548318$8_6.160046@attbi_s04...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:u8**************@TK2MSFTNGP10.phx.gbl...

<<clipped code block>>

As a note. If you want to remove quotes you'll have to process that here.
This particular algorithm will result in quoted strings being added to

your
DB.
kvPair[1].Trim().Trim('"'); would be sufficent, if mildly messy


Good point. I missed that detail.

now, if I just had Edit and Continue...
(just kidding :-)


LOL
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
1
by: Terry Olsen | last post by:
I download xml logs from several servers every day and read the data out of them using the XmlTextReader. But about 10% of them each day throw exceptions because they are not well formed. I don't...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
4
by: Morgan Cheng | last post by:
In my case, I have to remove any line containing "0.000000" from input string. In below case, it takes about 100 ms for 2k size input string. Regex.Replace(inputString, ".*0\\.000000.*\n", ""); I...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming...
4
by: Danny Ni | last post by:
Hi, The following code snippet is causing CPU to max out on my local machine and production servers. It looks fine on Expresso though. Regex rgxVideo = new...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.