By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Trim a multiple line message to a single line

P: n/a
This is an example of the data;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002 404.2.01 (6511)
RX 74 bytes
2007/07/27 11:00:03 [153006] 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13
48
2007/07/27 11:00:03 [153006] 52 00 52 02 02 C7 83 D7 07 07 1B 0B 00 00 00
00
2007/07/27 11:00:03 [153006] 28 0A 06 06 06 06 06 06 06 06 06 06 06 0A 06
06
2007/07/27 11:00:03 [153006] 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06
06
2007/07/27 11:00:03 [153006] 06 06 06 06 06 06 06 06 06 40
The Date/Time and [] information are the same for this one item.

I want to strip off all of the Date/Time and extra [] info and make one big
line.

Any help appreciated.
Aug 2 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Hello Brian,
2007/07/27 11:00:03 [153006]
My guess is that these aren't the same length for every line. If they were
it would be easy:

regex = "^.{num}";
input = "...";
replacement = "";
result = Regex.Replace(regex, input, replacement, RegexOptions.MultiLine);

If the part between [] is variable in length you'd have to choose a more
advanced way:

^[^\[]+\[[^\]]+\]

It's hard to read. But essentially it says:
^ start at the beginning of a line
[^\[]+ get everything that is not a [
\[ get the opening [
[^\]]+ get everything that is not a ]
\] get the closing ]

and replace that with an empty string.

should do the job.

Another option is to construct a strignbuilder, readline every line of the
original input, use substring of the first ] to chop of the first part and
add the resulting string to the stringbuilder.

The first solution (regex) is shorter and once you understand the regex easy
to understand and maintain.
The second solution (stringbuilder) is a bit more complex in lines of code,
easier to understand if you're not used to regex and probably a tad faster.

Jesse
Aug 2 '07 #2

P: n/a
Jesse the data inside of the [] is static six digits for each line.

The total number of lines may vary anywhere from one to as much as 8 lines
of data.

The line should look like this when pieced together;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002 404.2.01 (6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52 00 52 02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06 06 06 06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06
06 40

All in one big line without word wrap.

The next entry would be a different header (ARES_INDICATION for example).
"Jesse Houwing" wrote:
Hello Brian,
2007/07/27 11:00:03 [153006]

My guess is that these aren't the same length for every line. If they were
it would be easy:

regex = "^.{num}";
input = "...";
replacement = "";
result = Regex.Replace(regex, input, replacement, RegexOptions.MultiLine);

If the part between [] is variable in length you'd have to choose a more
advanced way:

^[^\[]+\[[^\]]+\]

It's hard to read. But essentially it says:
^ start at the beginning of a line
[^\[]+ get everything that is not a [
\[ get the opening [
[^\]]+ get everything that is not a ]
\] get the closing ]

and replace that with an empty string.

should do the job.

Another option is to construct a strignbuilder, readline every line of the
original input, use substring of the first ] to chop of the first part and
add the resulting string to the stringbuilder.

The first solution (regex) is shorter and once you understand the regex easy
to understand and maintain.
The second solution (stringbuilder) is a bit more complex in lines of code,
easier to understand if you're not used to regex and probably a tad faster.

Jesse

Aug 2 '07 #3

P: n/a
Hello Brian,

Could you send a sample file with two of these data blocks f what they should
look like and how you receive them directly to my email? I'll reply back
to this thread with a solution when I have time to go over it. My newsreader
keeps messing up your samples, so it would be easier if I had two text files.
Jesse

Jesse the data inside of the [] is static six digits for each line.

The total number of lines may vary anywhere from one to as much as 8
lines of data.

The line should look like this when pieced together;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002 404.2.01
(6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52 00 52
02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06 06 06
06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06
06 06 06 40

All in one big line without word wrap.

The next entry would be a different header (ARES_INDICATION for
example).

"Jesse Houwing" wrote:
>Hello Brian,
>>2007/07/27 11:00:03 [153006]
My guess is that these aren't the same length for every line. If they
were it would be easy:

regex = "^.{num}";
input = "...";
replacement = "";
result = Regex.Replace(regex, input, replacement,
RegexOptions.MultiLine);
If the part between [] is variable in length you'd have to choose a
more advanced way:

^[^\[]+\[[^\]]+\]

It's hard to read. But essentially it says:
^ start at the beginning of a line
[^\[]+ get everything that is not a [
\[ get the opening [
[^\]]+ get everything that is not a ]
\] get the closing ]
and replace that with an empty string.

should do the job.

Another option is to construct a strignbuilder, readline every line
of the original input, use substring of the first ] to chop of the
first part and add the resulting string to the stringbuilder.

The first solution (regex) is shorter and once you understand the
regex easy
to understand and maintain.
The second solution (stringbuilder) is a bit more complex in lines of
code,
easier to understand if you're not used to regex and probably a tad
faster.
Jesse

Aug 2 '07 #4

P: n/a
Sent you an email with a sampling.

"Jesse Houwing" wrote:
Hello Brian,

Could you send a sample file with two of these data blocks f what they should
look like and how you receive them directly to my email? I'll reply back
to this thread with a solution when I have time to go over it. My newsreader
keeps messing up your samples, so it would be easier if I had two text files.
Jesse

Jesse the data inside of the [] is static six digits for each line.

The total number of lines may vary anywhere from one to as much as 8
lines of data.

The line should look like this when pieced together;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002 404.2.01
(6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52 00 52
02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06 06 06
06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06
06 06 06 40

All in one big line without word wrap.

The next entry would be a different header (ARES_INDICATION for
example).

"Jesse Houwing" wrote:
Hello Brian,

2007/07/27 11:00:03 [153006]

My guess is that these aren't the same length for every line. If they
were it would be easy:

regex = "^.{num}";
input = "...";
replacement = "";
result = Regex.Replace(regex, input, replacement,
RegexOptions.MultiLine);
If the part between [] is variable in length you'd have to choose a
more advanced way:

^[^\[]+\[[^\]]+\]

It's hard to read. But essentially it says:
^ start at the beginning of a line
[^\[]+ get everything that is not a [
\[ get the opening [
[^\]]+ get everything that is not a ]
\] get the closing ]
and replace that with an empty string.

should do the job.

Another option is to construct a strignbuilder, readline every line
of the original input, use substring of the first ] to chop of the
first part and add the resulting string to the stringbuilder.

The first solution (regex) is shorter and once you understand the
regex easy
to understand and maintain.
The second solution (stringbuilder) is a bit more complex in lines of
code,
easier to understand if you're not used to regex and probably a tad
faster.
Jesse


Aug 2 '07 #5

P: n/a
Hello Brian,

You can do this quite easily with either a regex or a simple function I'll
try to demonstrate both:

basically you need a regex that matches the \r\ndate+time+[number] that is
followed by two spaces and replace it with a space

so the regex looks like this (I simplified it a bit, but given the fact that
all the lines are very well layed out I don't thing you have to make it more
differcult :).

\r\n[^\]]+\][ ]{2}

now replace this by " " and you're all set.

So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}", RegexOptions.Compiled);

private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}

Or you could use a StringReader/StringBuilder combination:

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); /* or simply continue; */ }

if (line[29] != ' ')
{
if (sb.Length 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}

I haven't used much error checking to make sure the format is correct. But
I think you can go on from here.

Jesse
Sent you an email with a sampling.

"Jesse Houwing" wrote:
>Hello Brian,

Could you send a sample file with two of these data blocks f what
they should look like and how you receive them directly to my email?
I'll reply back to this thread with a solution when I have time to go
over it. My newsreader keeps messing up your samples, so it would be
easier if I had two text files.

Jesse
>>Jesse the data inside of the [] is static six digits for each line.

The total number of lines may vary anywhere from one to as much as 8
lines of data.

The line should look like this when pieced together;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
404.2.01
(6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52 00
52
02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06 06 06
06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06
06
06 06 06 40
All in one big line without word wrap.

The next entry would be a different header (ARES_INDICATION for
example).

"Jesse Houwing" wrote:

Hello Brian,

2007/07/27 11:00:03 [153006]
>
My guess is that these aren't the same length for every line. If
they were it would be easy:

regex = "^.{num}";
input = "...";
replacement = "";
result = Regex.Replace(regex, input, replacement,
RegexOptions.MultiLine);
If the part between [] is variable in length you'd have to choose a
more advanced way:
^[^\[]+\[[^\]]+\]

It's hard to read. But essentially it says:
^ start at the beginning of a line
[^\[]+ get everything that is not a [
\[ get the opening [
[^\]]+ get everything that is not a ]
\] get the closing ]
and replace that with an empty string.
should do the job.

Another option is to construct a strignbuilder, readline every line
of the original input, use substring of the first ] to chop of the
first part and add the resulting string to the stringbuilder.

The first solution (regex) is shorter and once you understand the
regex easy
to understand and maintain.
The second solution (stringbuilder) is a bit more complex in lines
of
code,
easier to understand if you're not used to regex and probably a tad
faster.
Jesse

Aug 2 '07 #6

P: n/a
Thank you Jesse, that will help a bunch. I have C# classes scheduled
Aug/Sep/Oct so that I can try to avoid more questions in the future.

V/R

Brian

"Jesse Houwing" wrote:
Hello Brian,

You can do this quite easily with either a regex or a simple function I'll
try to demonstrate both:

basically you need a regex that matches the \r\ndate+time+[number] that is
followed by two spaces and replace it with a space

so the regex looks like this (I simplified it a bit, but given the fact that
all the lines are very well layed out I don't thing you have to make it more
differcult :).

\r\n[^\]]+\][ ]{2}

now replace this by " " and you're all set.

So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}", RegexOptions.Compiled);

private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}

Or you could use a StringReader/StringBuilder combination:

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); /* or simply continue; */ }

if (line[29] != ' ')
{
if (sb.Length 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}

I haven't used much error checking to make sure the format is correct. But
I think you can go on from here.

Jesse
Sent you an email with a sampling.

"Jesse Houwing" wrote:
Hello Brian,

Could you send a sample file with two of these data blocks f what
they should look like and how you receive them directly to my email?
I'll reply back to this thread with a solution when I have time to go
over it. My newsreader keeps messing up your samples, so it would be
easier if I had two text files.

Jesse

Jesse the data inside of the [] is static six digits for each line.

The total number of lines may vary anywhere from one to as much as 8
lines of data.

The line should look like this when pieced together;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
404.2.01
(6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52 00
52
02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06 06 06
06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06
06
06 06 06 40
All in one big line without word wrap.

The next entry would be a different header (ARES_INDICATION for
example).

"Jesse Houwing" wrote:

Hello Brian,

2007/07/27 11:00:03 [153006]

My guess is that these aren't the same length for every line. If
they were it would be easy:

regex = "^.{num}";
input = "...";
replacement = "";
result = Regex.Replace(regex, input, replacement,
RegexOptions.MultiLine);
If the part between [] is variable in length you'd have to choose a
more advanced way:
^[^\[]+\[[^\]]+\]

It's hard to read. But essentially it says:
^ start at the beginning of a line
[^\[]+ get everything that is not a [
\[ get the opening [
[^\]]+ get everything that is not a ]
\] get the closing ]
and replace that with an empty string.
should do the job.

Another option is to construct a strignbuilder, readline every line
of the original input, use substring of the first ] to chop of the
first part and add the resulting string to the stringbuilder.

The first solution (regex) is shorter and once you understand the
regex easy
to understand and maintain.
The second solution (stringbuilder) is a bit more complex in lines
of
code,
easier to understand if you're not used to regex and probably a tad
faster.
Jesse


Aug 2 '07 #7

P: n/a
Hello Brian,
Thank you Jesse, that will help a bunch. I have C# classes scheduled
Aug/Sep/Oct so that I can try to avoid more questions in the future.
You're very welcome and don't hesitate to ask questions that is what these
newsgroups are for after all. Just make sure you've tried ;)

Jesse

>
V/R

Brian

"Jesse Houwing" wrote:
>Hello Brian,

You can do this quite easily with either a regex or a simple function
I'll try to demonstrate both:

basically you need a regex that matches the \r\ndate+time+[number]
that is followed by two spaces and replace it with a space

so the regex looks like this (I simplified it a bit, but given the
fact that all the lines are very well layed out I don't thing you
have to make it more differcult :).

\r\n[^\]]+\][ ]{2}

now replace this by " " and you're all set.

So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
RegexOptions.Compiled);
private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}
Or you could use a StringReader/StringBuilder combination:

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); /* or simply continue; */ }
if (line[29] != ' ')
{
if (sb.Length 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
I haven't used much error checking to make sure the format is
correct. But I think you can go on from here.

Jesse
>>Sent you an email with a sampling.

"Jesse Houwing" wrote:

Hello Brian,

Could you send a sample file with two of these data blocks f what
they should look like and how you receive them directly to my
email? I'll reply back to this thread with a solution when I have
time to go over it. My newsreader keeps messing up your samples, so
it would be easier if I had two text files.

Jesse

Jesse the data inside of the [] is static six digits for each
line.
>
The total number of lines may vary anywhere from one to as much as
8 lines of data.
>
The line should look like this when pieced together;
>
2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
404.2.01
(6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52 00
52
02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06 06
06
06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06
06
06
06 06 06 40
All in one big line without word wrap.
The next entry would be a different header (ARES_INDICATION for
example).
>
"Jesse Houwing" wrote:
>
>Hello Brian,
>>
>>2007/07/27 11:00:03 [153006]
>>>
>My guess is that these aren't the same length for every line. If
>they were it would be easy:
>>
>regex = "^.{num}";
>input = "...";
>replacement = "";
>result = Regex.Replace(regex, input, replacement,
>RegexOptions.MultiLine);
>If the part between [] is variable in length you'd have to choose
>a
>more advanced way:
>^[^\[]+\[[^\]]+\]
>It's hard to read. But essentially it says:
>^ start at the beginning of a line
>[^\[]+ get everything that is not a [
>\[ get the opening [
>[^\]]+ get everything that is not a ]
>\] get the closing ]
>and replace that with an empty string.
>should do the job.
>Another option is to construct a strignbuilder, readline every
>line of the original input, use substring of the first ] to chop
>of the first part and add the resulting string to the
>stringbuilder.
>>
>The first solution (regex) is shorter and once you understand the
>regex easy
>to understand and maintain.
>The second solution (stringbuilder) is a bit more complex in
>lines
>of
>code,
>easier to understand if you're not used to regex and probably a
>tad
>faster.
>Jesse

Aug 2 '07 #8

P: n/a
Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every line that
starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the how I want
it to look the field would the C7 that follows the 02 02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:
Hello Brian,
Thank you Jesse, that will help a bunch. I have C# classes scheduled
Aug/Sep/Oct so that I can try to avoid more questions in the future.

You're very welcome and don't hesitate to ask questions that is what these
newsgroups are for after all. Just make sure you've tried ;)

Jesse


V/R

Brian

"Jesse Houwing" wrote:
Hello Brian,

You can do this quite easily with either a regex or a simple function
I'll try to demonstrate both:

basically you need a regex that matches the \r\ndate+time+[number]
that is followed by two spaces and replace it with a space

so the regex looks like this (I simplified it a bit, but given the
fact that all the lines are very well layed out I don't thing you
have to make it more differcult :).

\r\n[^\]]+\][ ]{2}

now replace this by " " and you're all set.

So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
RegexOptions.Compiled);
private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}
Or you could use a StringReader/StringBuilder combination:

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); /* or simply continue; */ }
if (line[29] != ' ')
{
if (sb.Length 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
I haven't used much error checking to make sure the format is
correct. But I think you can go on from here.

Jesse

Sent you an email with a sampling.

"Jesse Houwing" wrote:

Hello Brian,

Could you send a sample file with two of these data blocks f what
they should look like and how you receive them directly to my
email? I'll reply back to this thread with a solution when I have
time to go over it. My newsreader keeps messing up your samples, so
it would be easier if I had two text files.

Jesse

Jesse the data inside of the [] is static six digits for each
line.

The total number of lines may vary anywhere from one to as much as
8 lines of data.

The line should look like this when pieced together;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
404.2.01
(6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52 00
52
02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06 06
06
06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06
06
06
06 06 06 40
All in one big line without word wrap.
The next entry would be a different header (ARES_INDICATION for
example).

"Jesse Houwing" wrote:

Hello Brian,
>
>2007/07/27 11:00:03 [153006]
>>
My guess is that these aren't the same length for every line. If
they were it would be easy:
>
regex = "^.{num}";
input = "...";
replacement = "";
result = Regex.Replace(regex, input, replacement,
RegexOptions.MultiLine);
If the part between [] is variable in length you'd have to choose
a
more advanced way:
^[^\[]+\[[^\]]+\]
It's hard to read. But essentially it says:
^ start at the beginning of a line
[^\[]+ get everything that is not a [
\[ get the opening [
[^\]]+ get everything that is not a ]
\] get the closing ]
and replace that with an empty string.
should do the job.
Another option is to construct a strignbuilder, readline every
line of the original input, use substring of the first ] to chop
of the first part and add the resulting string to the
stringbuilder.
>
The first solution (regex) is shorter and once you understand the
regex easy
to understand and maintain.
The second solution (stringbuilder) is a bit more complex in
lines
of
code,
easier to understand if you're not used to regex and probably a
tad
faster.
Jesse


Aug 3 '07 #9

P: n/a
Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse
Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every line
that starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the how I
want it to look the field would the C7 that follows the 02 02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:
>Hello Brian,
>>Thank you Jesse, that will help a bunch. I have C# classes scheduled
Aug/Sep/Oct so that I can try to avoid more questions in the future.
You're very welcome and don't hesitate to ask questions that is what
these newsgroups are for after all. Just make sure you've tried ;)

Jesse
>>V/R

Brian

"Jesse Houwing" wrote:

Hello Brian,

You can do this quite easily with either a regex or a simple
function I'll try to demonstrate both:

basically you need a regex that matches the \r\ndate+time+[number]
that is followed by two spaces and replace it with a space

so the regex looks like this (I simplified it a bit, but given the
fact that all the lines are very well layed out I don't thing you
have to make it more differcult :).

\r\n[^\]]+\][ ]{2}

now replace this by " " and you're all set.

So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
RegexOptions.Compiled);
private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}
Or you could use a StringReader/StringBuilder combination:
private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid
input"); /* or simply continue; */ }
if (line[29] != ' ')
{
if (sb.Length 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
I haven't used much error checking to make sure the format is
correct. But I think you can go on from here.
Jesse

Sent you an email with a sampling.
>
"Jesse Houwing" wrote:
>
>Hello Brian,
>>
>Could you send a sample file with two of these data blocks f what
>they should look like and how you receive them directly to my
>email? I'll reply back to this thread with a solution when I have
>time to go over it. My newsreader keeps messing up your samples,
>so it would be easier if I had two text files.
>>
>Jesse
>>
>>Jesse the data inside of the [] is static six digits for each
>>line.
>>>
>>The total number of lines may vary anywhere from one to as much
>>as 8 lines of data.
>>>
>>The line should look like this when pieced together;
>>>
>>2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
>>404.2.01
>>(6511)
>>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52
>>00
>>52
>>02 02
>>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06
>>06
>>06
>>06 0A 06
>>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06
>>06
>>06
>>06 06 06 40
>>All in one big line without word wrap.
>>The next entry would be a different header (ARES_INDICATION for
>>example).
>>"Jesse Houwing" wrote:
>>>
>>>Hello Brian,
>>>>
>>>>2007/07/27 11:00:03 [153006]
>>>>>
>>>My guess is that these aren't the same length for every line.
>>>If they were it would be easy:
>>>>
>>>regex = "^.{num}";
>>>input = "...";
>>>replacement = "";
>>>result = Regex.Replace(regex, input, replacement,
>>>RegexOptions.MultiLine);
>>>If the part between [] is variable in length you'd have to
>>>choose
>>>a
>>>more advanced way:
>>>^[^\[]+\[[^\]]+\]
>>>It's hard to read. But essentially it says:
>>>^ start at the beginning of a line
>>>[^\[]+ get everything that is not a [
>>>\[ get the opening [
>>>[^\]]+ get everything that is not a ]
>>>\] get the closing ]
>>>and replace that with an empty string.
>>>should do the job.
>>>Another option is to construct a strignbuilder, readline every
>>>line of the original input, use substring of the first ] to
>>>chop
>>>of the first part and add the resulting string to the
>>>stringbuilder.
>>>The first solution (regex) is shorter and once you understand
>>>the
>>>regex easy
>>>to understand and maintain.
>>>The second solution (stringbuilder) is a bit more complex in
>>>lines
>>>of
>>>code,
>>>easier to understand if you're not used to regex and probably a
>>>tad
>>>faster.
>>>Jesse

Aug 3 '07 #10

P: n/a
I wnt with the Stringbuilder solution. It seemed easier to follow to me than
the Regex one.

"Jesse Houwing" wrote:
Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse
Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every line
that starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the how I
want it to look the field would the C7 that follows the 02 02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:
Hello Brian,

Thank you Jesse, that will help a bunch. I have C# classes scheduled
Aug/Sep/Oct so that I can try to avoid more questions in the future.

You're very welcome and don't hesitate to ask questions that is what
these newsgroups are for after all. Just make sure you've tried ;)

Jesse

V/R

Brian

"Jesse Houwing" wrote:

Hello Brian,

You can do this quite easily with either a regex or a simple
function I'll try to demonstrate both:

basically you need a regex that matches the \r\ndate+time+[number]
that is followed by two spaces and replace it with a space

so the regex looks like this (I simplified it a bit, but given the
fact that all the lines are very well layed out I don't thing you
have to make it more differcult :).

\r\n[^\]]+\][ ]{2}

now replace this by " " and you're all set.

So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
RegexOptions.Compiled);
private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}
Or you could use a StringReader/StringBuilder combination:
private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid
input"); /* or simply continue; */ }
if (line[29] != ' ')
{
if (sb.Length 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
I haven't used much error checking to make sure the format is
correct. But I think you can go on from here.
Jesse

Sent you an email with a sampling.

"Jesse Houwing" wrote:

Hello Brian,
>
Could you send a sample file with two of these data blocks f what
they should look like and how you receive them directly to my
email? I'll reply back to this thread with a solution when I have
time to go over it. My newsreader keeps messing up your samples,
so it would be easier if I had two text files.
>
Jesse
>
>Jesse the data inside of the [] is static six digits for each
>line.
>>
>The total number of lines may vary anywhere from one to as much
>as 8 lines of data.
>>
>The line should look like this when pieced together;
>>
>2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
>404.2.01
>(6511)
>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48 52
>00
>52
>02 02
>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06
>06
>06
>06 0A 06
>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06 06
>06
>06
>06 06 06 40
>All in one big line without word wrap.
>The next entry would be a different header (ARES_INDICATION for
>example).
>"Jesse Houwing" wrote:
>>
>>Hello Brian,
>>>
>>>2007/07/27 11:00:03 [153006]
>>>>
>>My guess is that these aren't the same length for every line.
>>If they were it would be easy:
>>>
>>regex = "^.{num}";
>>input = "...";
>>replacement = "";
>>result = Regex.Replace(regex, input, replacement,
>>RegexOptions.MultiLine);
>>If the part between [] is variable in length you'd have to
>>choose
>>a
>>more advanced way:
>>^[^\[]+\[[^\]]+\]
>>It's hard to read. But essentially it says:
>>^ start at the beginning of a line
>>[^\[]+ get everything that is not a [
>>\[ get the opening [
>>[^\]]+ get everything that is not a ]
>>\] get the closing ]
>>and replace that with an empty string.
>>should do the job.
>>Another option is to construct a strignbuilder, readline every
>>line of the original input, use substring of the first ] to
>>chop
>>of the first part and add the resulting string to the
>>stringbuilder.
>>The first solution (regex) is shorter and once you understand
>>the
>>regex easy
>>to understand and maintain.
>>The second solution (stringbuilder) is a bit more complex in
>>lines
>>of
>>code,
>>easier to understand if you're not used to regex and probably a
>>tad
>>faster.
>>Jesse


Aug 3 '07 #11

P: n/a
Hello Brian,

I have no clue how to give text a different colour. I am sure though that
you would need to use a RichTextBox. I'd guess you'd have to insert some
RTF control characters. Where to insert them would be easy to find out if
it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}

It's all in one function now, but I'd place the extra checks in a subfunction
should they grow any further.

I tried to find a string function that automatically gives the xth position
of a substring (IndexOf(what, from, count, number) but I couldn't find it...
too bad ;)).
Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpair >[A-F0-9]{2})", "$1" +
"control character for colour" + "$2" + "control character for normal colour");

It's actually easier for me to read than the function above, but that is
partly due to the fact that I'm presenting courses in .NET Regular Expressions
as part of my job.

Jesse
I wnt with the Stringbuilder solution. It seemed easier to follow to
me than the Regex one.

"Jesse Houwing" wrote:
>Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse
>>Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every
line that starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the how
I want it to look the field would the C7 that follows the 02 02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:

Hello Brian,

Thank you Jesse, that will help a bunch. I have C# classes
scheduled Aug/Sep/Oct so that I can try to avoid more questions in
the future.
>
You're very welcome and don't hesitate to ask questions that is
what these newsgroups are for after all. Just make sure you've
tried ;)

Jesse

V/R
>
Brian
>
"Jesse Houwing" wrote:
>
>Hello Brian,
>>
>You can do this quite easily with either a regex or a simple
>function I'll try to demonstrate both:
>>
>basically you need a regex that matches the
>\r\ndate+time+[number] that is followed by two spaces and replace
>it with a space
>>
>so the regex looks like this (I simplified it a bit, but given
>the fact that all the lines are very well layed out I don't thing
>you have to make it more differcult :).
>>
>\r\n[^\]]+\][ ]{2}
>>
>now replace this by " " and you're all set.
>>
>So:
>private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
>RegexOptions.Compiled);
>private string LayoutInput(string input)
>{
>return _rx.Replace(input, " ");
>}
>Or you could use a StringReader/StringBuilder combination:
>private string LayoutInput(string input)
>{
>StringReader sr = new StringReader(input);
>StringBuilder sb = new StringBuilder(input.Length);
>string line;
>while ((line = sr.ReadLine())!= null)
>{
>if (line.Length < 29) { throw new
>InvalidOperationException("invalid
>input"); /* or simply continue; */ }
>if (line[29] != ' ')
>{
>if (sb.Length 0)
>{
>sb.Append("\r\n");
>}
>sb.Append(line);
>}
>else
>{
>sb.Append(line.Substring(30));
>}
>}
>return sb.ToString();
>}
>I haven't used much error checking to make sure the format is
>correct. But I think you can go on from here.
>Jesse
>>Sent you an email with a sampling.
>>>
>>"Jesse Houwing" wrote:
>>>
>>>Hello Brian,
>>>>
>>>Could you send a sample file with two of these data blocks f
>>>what they should look like and how you receive them directly to
>>>my email? I'll reply back to this thread with a solution when I
>>>have time to go over it. My newsreader keeps messing up your
>>>samples, so it would be easier if I had two text files.
>>>>
>>>Jesse
>>>>
>>>>Jesse the data inside of the [] is static six digits for each
>>>>line.
>>>>>
>>>>The total number of lines may vary anywhere from one to as
>>>>much as 8 lines of data.
>>>>>
>>>>The line should look like this when pieced together;
>>>>>
>>>>2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
>>>>404.2.01
>>>>(6511)
>>>>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48
>>>>52
>>>>00
>>>>52
>>>>02 02
>>>>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06
>>>>06
>>>>06
>>>>06 0A 06
>>>>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06
>>>>06
>>>>06
>>>>06
>>>>06 06 06 40
>>>>All in one big line without word wrap.
>>>>The next entry would be a different header (ARES_INDICATION
>>>>for
>>>>example).
>>>>"Jesse Houwing" wrote:
>>>>>Hello Brian,
>>>>>>
>>>>>>2007/07/27 11:00:03 [153006]
>>>>>>>
>>>>>My guess is that these aren't the same length for every line.
>>>>>If they were it would be easy:
>>>>>>
>>>>>regex = "^.{num}";
>>>>>input = "...";
>>>>>replacement = "";
>>>>>result = Regex.Replace(regex, input, replacement,
>>>>>RegexOptions.MultiLine);
>>>>>If the part between [] is variable in length you'd have to
>>>>>choose
>>>>>a
>>>>>more advanced way:
>>>>>^[^\[]+\[[^\]]+\]
>>>>>It's hard to read. But essentially it says:
>>>>>^ start at the beginning of a line
>>>>>[^\[]+ get everything that is not a [
>>>>>\[ get the opening [
>>>>>[^\]]+ get everything that is not a ]
>>>>>\] get the closing ]
>>>>>and replace that with an empty string.
>>>>>should do the job.
>>>>>Another option is to construct a strignbuilder, readline
>>>>>every
>>>>>line of the original input, use substring of the first ] to
>>>>>chop
>>>>>of the first part and add the resulting string to the
>>>>>stringbuilder.
>>>>>The first solution (regex) is shorter and once you understand
>>>>>the
>>>>>regex easy
>>>>>to understand and maintain.
>>>>>The second solution (stringbuilder) is a bit more complex in
>>>>>lines
>>>>>of
>>>>>code,
>>>>>easier to understand if you're not used to regex and probably
>>>>>a
>>>>>tad
>>>>>faster.
>>>>>Jesse

Aug 3 '07 #12

P: n/a
Jesse, I appreciate all your help so far, and have now come up with something
I am once again unsure how to fix.;

In the Example.txt file I sent you, when the lines are combined into one
they are a little jumbled. I would like to format the line so that the RX and
TX portion all start at the 78th position from the left. Everything to the
left of that would never go past the 75th position.

I know that I can perform a StringBuilder.Insert, just not sure how or where
to add it into this code.

Here is the code for what we made work.
---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')

{
sb.Append("\r\n");
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---

Thanks,

"Jesse Houwing" wrote:
Hello Brian,

I have no clue how to give text a different colour. I am sure though that
you would need to use a RichTextBox. I'd guess you'd have to insert some
RTF control characters. Where to insert them would be easy to find out if
it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}

It's all in one function now, but I'd place the extra checks in a subfunction
should they grow any further.

I tried to find a string function that automatically gives the xth position
of a substring (IndexOf(what, from, count, number) but I couldn't find it...
too bad ;)).
Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpair >[A-F0-9]{2})", "$1" +
"control character for colour" + "$2" + "control character for normal colour");

It's actually easier for me to read than the function above, but that is
partly due to the fact that I'm presenting courses in .NET Regular Expressions
as part of my job.

Jesse
I wnt with the Stringbuilder solution. It seemed easier to follow to
me than the Regex one.

"Jesse Houwing" wrote:
Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse

Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every
line that starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the how
I want it to look the field would the C7 that follows the 02 02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:

Hello Brian,

Thank you Jesse, that will help a bunch. I have C# classes
scheduled Aug/Sep/Oct so that I can try to avoid more questions in
the future.

You're very welcome and don't hesitate to ask questions that is
what these newsgroups are for after all. Just make sure you've
tried ;)

Jesse

V/R

Brian

"Jesse Houwing" wrote:

Hello Brian,
>
You can do this quite easily with either a regex or a simple
function I'll try to demonstrate both:
>
basically you need a regex that matches the
\r\ndate+time+[number] that is followed by two spaces and replace
it with a space
>
so the regex looks like this (I simplified it a bit, but given
the fact that all the lines are very well layed out I don't thing
you have to make it more differcult :).
>
\r\n[^\]]+\][ ]{2}
>
now replace this by " " and you're all set.
>
So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
RegexOptions.Compiled);
private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}
Or you could use a StringReader/StringBuilder combination:
private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid
input"); /* or simply continue; */ }
if (line[29] != ' ')
{
if (sb.Length 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
I haven't used much error checking to make sure the format is
correct. But I think you can go on from here.
Jesse
>Sent you an email with a sampling.
>>
>"Jesse Houwing" wrote:
>>
>>Hello Brian,
>>>
>>Could you send a sample file with two of these data blocks f
>>what they should look like and how you receive them directly to
>>my email? I'll reply back to this thread with a solution when I
>>have time to go over it. My newsreader keeps messing up your
>>samples, so it would be easier if I had two text files.
>>>
>>Jesse
>>>
>>>Jesse the data inside of the [] is static six digits for each
>>>line.
>>>>
>>>The total number of lines may vary anywhere from one to as
>>>much as 8 lines of data.
>>>>
>>>The line should look like this when pieced together;
>>>>
>>>2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
>>>404.2.01
>>>(6511)
>>>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48
>>>52
>>>00
>>>52
>>>02 02
>>>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06 06
>>>06
>>>06
>>>06 0A 06
>>>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06 06
>>>06
>>>06
>>>06
>>>06 06 06 40
>>>All in one big line without word wrap.
>>>The next entry would be a different header (ARES_INDICATION
>>>for
>>>example).
>>>"Jesse Houwing" wrote:
>>>>Hello Brian,
>>>>>
>>>>>2007/07/27 11:00:03 [153006]
>>>>>>
>>>>My guess is that these aren't the same length for every line.
>>>>If they were it would be easy:
>>>>>
>>>>regex = "^.{num}";
>>>>input = "...";
>>>>replacement = "";
>>>>result = Regex.Replace(regex, input, replacement,
>>>>RegexOptions.MultiLine);
>>>>If the part between [] is variable in length you'd have to
>>>>choose
>>>>a
>>>>more advanced way:
>>>>^[^\[]+\[[^\]]+\]
>>>>It's hard to read. But essentially it says:
>>>>^ start at the beginning of a line
>>>>[^\[]+ get everything that is not a [
>>>>\[ get the opening [
>>>>[^\]]+ get everything that is not a ]
>>>>\] get the closing ]
>>>>and replace that with an empty string.
>>>>should do the job.
>>>>Another option is to construct a strignbuilder, readline
>>>>every
>>>>line of the original input, use substring of the first ] to
>>>>chop
>>>>of the first part and add the resulting string to the
>>>>stringbuilder.
>>>>The first solution (regex) is shorter and once you understand
>>>>the
>>>>regex easy
>>>>to understand and maintain.
>>>>The second solution (stringbuilder) is a bit more complex in
>>>>lines
>>>>of
>>>>code,
>>>>easier to understand if you're not used to regex and probably
>>>>a
>>>>tad
>>>>faster.
>>>>Jesse


Aug 5 '07 #13

P: n/a
Hello Brian,
Jesse, I appreciate all your help so far, and have now come up with
something I am once again unsure how to fix.;

In the Example.txt file I sent you, when the lines are combined into
one they are a little jumbled. I would like to format the line so that
the RX and TX portion all start at the 78th position from the left.
Everything to the left of that would never go past the 75th position.

I know that I can perform a StringBuilder.Insert, just not sure how or
where to add it into this code.

Here is the code for what we made work.
Basically you have two options.
1) Find the location and insert as many spaces as needed.
2) Find the location, split the string. Now use stringBuilder.AppendFormat
function
if you use the following format: "{0, -78}{1}" and the two parts of the string
you have collected using substring it will padd the string to 78 positions
if needed:

/* might need more specific searching for RX/TX */
if (line.InfexOf("TX") 0 || line.InfexOf("RX") 0)
{
int charactersTillPoint = /* Find out the position */
string part0 = line.SubString(0, charactersTillPoint);
string part1 = line.SubString(charactersTillPoint);
sb.AppendFormat("{0, -78}{1}", part0, part1);
}
I've inserted where to mark it with #####
---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
sb.Append("\r\n");

#### This is where to insert
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---

Thanks,

"Jesse Houwing" wrote:
>Hello Brian,

I have no clue how to give text a different colour. I am sure though
that you would need to use a RichTextBox. I'd guess you'd have to
insert some RTF control characters. Where to insert them would be
easy to find out if it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}
It's all in one function now, but I'd place the extra checks in a
subfunction should they grow any further.

I tried to find a string function that automatically gives the xth
position of a substring (IndexOf(what, from, count, number) but I
couldn't find it... too bad ;)).

Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpai r>[A-F0-9]{2})",
"$1" + "control character for colour" + "$2" + "control character for
normal colour");

It's actually easier for me to read than the function above, but that
is partly due to the fact that I'm presenting courses in .NET Regular
Expressions as part of my job.

Jesse
>>I wnt with the Stringbuilder solution. It seemed easier to follow to
me than the Regex one.

"Jesse Houwing" wrote:

Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse

Ok, Jessee, I have a new Wrinkle for you.
>
Lets say I want a specific hex pair highlighted in Blue on every
line that starts out with ARES_INDICATION or ARES_EINDICATION.
>
It would be the 34 item from the left.
>
If you have the Example.txt I emailed you, the last line in the
how I want it to look the field would the C7 that follows the 02
02 hex.
>
If not I can send it to you again.
>
"Jesse Houwing" wrote:
>
>Hello Brian,
>>
>>Thank you Jesse, that will help a bunch. I have C# classes
>>scheduled Aug/Sep/Oct so that I can try to avoid more questions
>>in the future.
>>>
>You're very welcome and don't hesitate to ask questions that is
>what these newsgroups are for after all. Just make sure you've
>tried ;)
>>
>Jesse
>>
>>V/R
>>>
>>Brian
>>>
>>"Jesse Houwing" wrote:
>>>
>>>Hello Brian,
>>>>
>>>You can do this quite easily with either a regex or a simple
>>>function I'll try to demonstrate both:
>>>>
>>>basically you need a regex that matches the
>>>\r\ndate+time+[number] that is followed by two spaces and
>>>replace it with a space
>>>>
>>>so the regex looks like this (I simplified it a bit, but given
>>>the fact that all the lines are very well layed out I don't
>>>thing you have to make it more differcult :).
>>>>
>>>\r\n[^\]]+\][ ]{2}
>>>>
>>>now replace this by " " and you're all set.
>>>>
>>>So:
>>>private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
>>>RegexOptions.Compiled);
>>>private string LayoutInput(string input)
>>>{
>>>return _rx.Replace(input, " ");
>>>}
>>>Or you could use a StringReader/StringBuilder combination:
>>>private string LayoutInput(string input)
>>>{
>>>StringReader sr = new StringReader(input);
>>>StringBuilder sb = new StringBuilder(input.Length);
>>>string line;
>>>while ((line = sr.ReadLine())!= null)
>>>{
>>>if (line.Length < 29) { throw new
>>>InvalidOperationException("invalid
>>>input"); /* or simply continue; */ }
>>>if (line[29] != ' ')
>>>{
>>>if (sb.Length 0)
>>>{
>>>sb.Append("\r\n");
>>>}
>>>sb.Append(line);
>>>}
>>>else
>>>{
>>>sb.Append(line.Substring(30));
>>>}
>>>}
>>>return sb.ToString();
>>>}
>>>I haven't used much error checking to make sure the format is
>>>correct. But I think you can go on from here.
>>>Jesse
>>>>Sent you an email with a sampling.
>>>>>
>>>>"Jesse Houwing" wrote:
>>>>>
>>>>>Hello Brian,
>>>>>>
>>>>>Could you send a sample file with two of these data blocks f
>>>>>what they should look like and how you receive them directly
>>>>>to my email? I'll reply back to this thread with a solution
>>>>>when I have time to go over it. My newsreader keeps messing
>>>>>up your samples, so it would be easier if I had two text
>>>>>files.
>>>>>>
>>>>>Jesse
>>>>>>
>>>>>>Jesse the data inside of the [] is static six digits for
>>>>>>each line.
>>>>>>>
>>>>>>The total number of lines may vary anywhere from one to as
>>>>>>much as 8 lines of data.
>>>>>>>
>>>>>>The line should look like this when pieced together;
>>>>>>>
>>>>>>2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
>>>>>>404.2.01
>>>>>>(6511)
>>>>>>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48
>>>>>>52
>>>>>>00
>>>>>>52
>>>>>>02 02
>>>>>>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06
>>>>>>06
>>>>>>06
>>>>>>06
>>>>>>06 0A 06
>>>>>>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06
>>>>>>06
>>>>>>06
>>>>>>06
>>>>>>06
>>>>>>06 06 06 40
>>>>>>All in one big line without word wrap.
>>>>>>The next entry would be a different header (ARES_INDICATION
>>>>>>for
>>>>>>example).
>>>>>>"Jesse Houwing" wrote:
>>>>>>>Hello Brian,
>>>>>>>>
>>>>>>>>2007/07/27 11:00:03 [153006]
>>>>>>>>>
>>>>>>>My guess is that these aren't the same length for every
>>>>>>>line. If they were it would be easy:
>>>>>>>>
>>>>>>>regex = "^.{num}";
>>>>>>>input = "...";
>>>>>>>replacement = "";
>>>>>>>result = Regex.Replace(regex, input, replacement,
>>>>>>>RegexOptions.MultiLine);
>>>>>>>If the part between [] is variable in length you'd have to
>>>>>>>choose
>>>>>>>a
>>>>>>>more advanced way:
>>>>>>>^[^\[]+\[[^\]]+\]
>>>>>>>It's hard to read. But essentially it says:
>>>>>>>^ start at the beginning of a line
>>>>>>>[^\[]+ get everything that is not a [
>>>>>>>\[ get the opening [
>>>>>>>[^\]]+ get everything that is not a ]
>>>>>>>\] get the closing ]
>>>>>>>and replace that with an empty string.
>>>>>>>should do the job.
>>>>>>>Another option is to construct a strignbuilder, readline
>>>>>>>every
>>>>>>>line of the original input, use substring of the first ] to
>>>>>>>chop
>>>>>>>of the first part and add the resulting string to the
>>>>>>>stringbuilder.
>>>>>>>The first solution (regex) is shorter and once you
>>>>>>>understand
>>>>>>>the
>>>>>>>regex easy
>>>>>>>to understand and maintain.
>>>>>>>The second solution (stringbuilder) is a bit more complex
>>>>>>>in
>>>>>>>lines
>>>>>>>of
>>>>>>>code,
>>>>>>>easier to understand if you're not used to regex and
>>>>>>>probably
>>>>>>>a
>>>>>>>tad
>>>>>>>faster.
>>>>>>>Jesse

Aug 5 '07 #14

P: n/a
Jesse, Here is what I did to get it working. At first I didn't realize that I
needed to declare the int. Because of the position of the TX in some lines I
had to move the format out to the 86th pos.

Thanks a bunch!

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
bool firstLine = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
if (firstLine)
firstLine = false;
else
sb.Append("\r\n");
if (((txPos = line.IndexOf("TX")) -1) || ((rxPos =
line.IndexOf("RX")) 0))
{
int charactersTillPoint;
if (txPos -1)
charactersTillPoint = txPos;
else
charactersTillPoint = rxPos;
string part0 = line.Substring(0, charactersTillPoint);
string part1 = line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}

"Jesse Houwing" wrote:
Hello Brian,
Jesse, I appreciate all your help so far, and have now come up with
something I am once again unsure how to fix.;

In the Example.txt file I sent you, when the lines are combined into
one they are a little jumbled. I would like to format the line so that
the RX and TX portion all start at the 78th position from the left.
Everything to the left of that would never go past the 75th position.

I know that I can perform a StringBuilder.Insert, just not sure how or
where to add it into this code.

Here is the code for what we made work.

Basically you have two options.
1) Find the location and insert as many spaces as needed.
2) Find the location, split the string. Now use stringBuilder.AppendFormat
function
if you use the following format: "{0, -78}{1}" and the two parts of the string
you have collected using substring it will padd the string to 78 positions
if needed:

/* might need more specific searching for RX/TX */
if (line.InfexOf("TX") 0 || line.InfexOf("RX") 0)
{
int charactersTillPoint = /* Find out the position */
string part0 = line.SubString(0, charactersTillPoint);
string part1 = line.SubString(charactersTillPoint);
sb.AppendFormat("{0, -78}{1}", part0, part1);
}
I've inserted where to mark it with #####
---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
sb.Append("\r\n");


#### This is where to insert
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---

Thanks,

"Jesse Houwing" wrote:
Hello Brian,

I have no clue how to give text a different colour. I am sure though
that you would need to use a RichTextBox. I'd guess you'd have to
insert some RTF control characters. Where to insert them would be
easy to find out if it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}
It's all in one function now, but I'd place the extra checks in a
subfunction should they grow any further.

I tried to find a string function that automatically gives the xth
position of a substring (IndexOf(what, from, count, number) but I
couldn't find it... too bad ;)).

Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpair >[A-F0-9]{2})",
"$1" + "control character for colour" + "$2" + "control character for
normal colour");

It's actually easier for me to read than the function above, but that
is partly due to the fact that I'm presenting courses in .NET Regular
Expressions as part of my job.

Jesse

I wnt with the Stringbuilder solution. It seemed easier to follow to
me than the Regex one.

"Jesse Houwing" wrote:

Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse

Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every
line that starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the
how I want it to look the field would the C7 that follows the 02
02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:

Hello Brian,
>
>Thank you Jesse, that will help a bunch. I have C# classes
>scheduled Aug/Sep/Oct so that I can try to avoid more questions
>in the future.
>>
You're very welcome and don't hesitate to ask questions that is
what these newsgroups are for after all. Just make sure you've
tried ;)
>
Jesse
>
>V/R
>>
>Brian
>>
>"Jesse Houwing" wrote:
>>
>>Hello Brian,
>>>
>>You can do this quite easily with either a regex or a simple
>>function I'll try to demonstrate both:
>>>
>>basically you need a regex that matches the
>>\r\ndate+time+[number] that is followed by two spaces and
>>replace it with a space
>>>
>>so the regex looks like this (I simplified it a bit, but given
>>the fact that all the lines are very well layed out I don't
>>thing you have to make it more differcult :).
>>>
>>\r\n[^\]]+\][ ]{2}
>>>
>>now replace this by " " and you're all set.
>>>
>>So:
>>private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
>>RegexOptions.Compiled);
>>private string LayoutInput(string input)
>>{
>>return _rx.Replace(input, " ");
>>}
>>Or you could use a StringReader/StringBuilder combination:
>>private string LayoutInput(string input)
>>{
>>StringReader sr = new StringReader(input);
>>StringBuilder sb = new StringBuilder(input.Length);
>>string line;
>>while ((line = sr.ReadLine())!= null)
>>{
>>if (line.Length < 29) { throw new
>>InvalidOperationException("invalid
>>input"); /* or simply continue; */ }
>>if (line[29] != ' ')
>>{
>>if (sb.Length 0)
>>{
>>sb.Append("\r\n");
>>}
>>sb.Append(line);
>>}
>>else
>>{
>>sb.Append(line.Substring(30));
>>}
>>}
>>return sb.ToString();
>>}
>>I haven't used much error checking to make sure the format is
>>correct. But I think you can go on from here.
>>Jesse
>>>Sent you an email with a sampling.
>>>>
>>>"Jesse Houwing" wrote:
>>>>
>>>>Hello Brian,
>>>>>
>>>>Could you send a sample file with two of these data blocks f
>>>>what they should look like and how you receive them directly
>>>>to my email? I'll reply back to this thread with a solution
>>>>when I have time to go over it. My newsreader keeps messing
>>>>up your samples, so it would be easier if I had two text
>>>>files.
>>>>>
>>>>Jesse
>>>>>
>>>>>Jesse the data inside of the [] is static six digits for
>>>>>each line.
>>>>>>
>>>>>The total number of lines may vary anywhere from one to as
>>>>>much as 8 lines of data.
>>>>>>
>>>>>The line should look like this when pieced together;
>>>>>>
>>>>>2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
>>>>>404.2.01
>>>>>(6511)
>>>>>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48
>>>>>52
>>>>>00
>>>>>52
>>>>>02 02
>>>>>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06
>>>>>06
>>>>>06
>>>>>06
>>>>>06 0A 06
>>>>>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06
>>>>>06
>>>>>06
>>>>>06
>>>>>06
>>>>>06 06 06 40
>>>>>All in one big line without word wrap.
>>>>>The next entry would be a different header (ARES_INDICATION
>>>>>for
>>>>>example).
>>>>>"Jesse Houwing" wrote:
>>>>>>Hello Brian,
>>>>>>>
>>>>>>>2007/07/27 11:00:03 [153006]
>>>>>>>>
>>>>>>My guess is that these aren't the same length for every
>>>>>>line. If they were it would be easy:
Aug 6 '07 #15

P: n/a
Jesse I thought I would let you know how I did the highlight/change color of
specific text;

private void findSequenceNumbers()
{
toolStripStatusLabel.Visible = toolStripProgressBar.Visible =
true;
toolStripProgressBar.Value = 0;
int lineNum = 0;
bool startingNewLine = true;
FontStyle style = FontStyle.Bold;
string[] lines = rtbTextBox.Lines;
string text = rtbTextBox.Text;
toolStripProgressBar.Maximum = text.Length;
for (int i = 0; i < text.Length; i++)
{
if (startingNewLine)
{
if ((lines[lineNum].Contains("ARES_EINDICATION")) ||
(lines[lineNum].Contains("ARES_INDICATION")))
{
i += 169;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^ style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else if
(lines[lineNum].Contains("]CODELINE_INDICATION_MSG"))
{
i += 160;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^ style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else
{
i += lines[lineNum].Length - 1;
}
startingNewLine = false;
Application.DoEvents();
}
if (text[i] == '\n')
{
startingNewLine = true;
lineNum++;
}
toolStripProgressBar.Value = i;
}
toolStripStatusLabel.Visible = toolStripProgressBar.Visible =
false;
}
Many Thanks,

Brian

"Jesse Houwing" wrote:
Hello Brian,
Jesse, I appreciate all your help so far, and have now come up with
something I am once again unsure how to fix.;

In the Example.txt file I sent you, when the lines are combined into
one they are a little jumbled. I would like to format the line so that
the RX and TX portion all start at the 78th position from the left.
Everything to the left of that would never go past the 75th position.

I know that I can perform a StringBuilder.Insert, just not sure how or
where to add it into this code.

Here is the code for what we made work.

Basically you have two options.
1) Find the location and insert as many spaces as needed.
2) Find the location, split the string. Now use stringBuilder.AppendFormat
function
if you use the following format: "{0, -78}{1}" and the two parts of the string
you have collected using substring it will padd the string to 78 positions
if needed:

/* might need more specific searching for RX/TX */
if (line.InfexOf("TX") 0 || line.InfexOf("RX") 0)
{
int charactersTillPoint = /* Find out the position */
string part0 = line.SubString(0, charactersTillPoint);
string part1 = line.SubString(charactersTillPoint);
sb.AppendFormat("{0, -78}{1}", part0, part1);
}
I've inserted where to mark it with #####
---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
sb.Append("\r\n");


#### This is where to insert
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---

Thanks,

"Jesse Houwing" wrote:
Hello Brian,

I have no clue how to give text a different colour. I am sure though
that you would need to use a RichTextBox. I'd guess you'd have to
insert some RTF control characters. Where to insert them would be
easy to find out if it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}
It's all in one function now, but I'd place the extra checks in a
subfunction should they grow any further.

I tried to find a string function that automatically gives the xth
position of a substring (IndexOf(what, from, count, number) but I
couldn't find it... too bad ;)).

Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpair >[A-F0-9]{2})",
"$1" + "control character for colour" + "$2" + "control character for
normal colour");

It's actually easier for me to read than the function above, but that
is partly due to the fact that I'm presenting courses in .NET Regular
Expressions as part of my job.

Jesse

I wnt with the Stringbuilder solution. It seemed easier to follow to
me than the Regex one.

"Jesse Houwing" wrote:

Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse

Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every
line that starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the
how I want it to look the field would the C7 that follows the 02
02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:

Hello Brian,
>
>Thank you Jesse, that will help a bunch. I have C# classes
>scheduled Aug/Sep/Oct so that I can try to avoid more questions
>in the future.
>>
You're very welcome and don't hesitate to ask questions that is
what these newsgroups are for after all. Just make sure you've
tried ;)
>
Jesse
>
>V/R
>>
>Brian
>>
>"Jesse Houwing" wrote:
>>
>>Hello Brian,
>>>
>>You can do this quite easily with either a regex or a simple
>>function I'll try to demonstrate both:
>>>
>>basically you need a regex that matches the
>>\r\ndate+time+[number] that is followed by two spaces and
>>replace it with a space
>>>
>>so the regex looks like this (I simplified it a bit, but given
>>the fact that all the lines are very well layed out I don't
>>thing you have to make it more differcult :).
>>>
>>\r\n[^\]]+\][ ]{2}
>>>
>>now replace this by " " and you're all set.
>>>
>>So:
>>private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
>>RegexOptions.Compiled);
>>private string LayoutInput(string input)
>>{
>>return _rx.Replace(input, " ");
>>}
>>Or you could use a StringReader/StringBuilder combination:
>>private string LayoutInput(string input)
>>{
>>StringReader sr = new StringReader(input);
>>StringBuilder sb = new StringBuilder(input.Length);
>>string line;
>>while ((line = sr.ReadLine())!= null)
>>{
>>if (line.Length < 29) { throw new
>>InvalidOperationException("invalid
>>input"); /* or simply continue; */ }
>>if (line[29] != ' ')
>>{
>>if (sb.Length 0)
>>{
>>sb.Append("\r\n");
>>}
>>sb.Append(line);
>>}
>>else
>>{
>>sb.Append(line.Substring(30));
>>}
>>}
>>return sb.ToString();
>>}
>>I haven't used much error checking to make sure the format is
>>correct. But I think you can go on from here.
>>Jesse
>>>Sent you an email with a sampling.
>>>>
>>>"Jesse Houwing" wrote:
>>>>
>>>>Hello Brian,
>>>>>
>>>>Could you send a sample file with two of these data blocks f
>>>>what they should look like and how you receive them directly
>>>>to my email? I'll reply back to this thread with a solution
>>>>when I have time to go over it. My newsreader keeps messing
>>>>up your samples, so it would be easier if I had two text
>>>>files.
>>>>>
>>>>Jesse
>>>>>
>>>>>Jesse the data inside of the [] is static six digits for
>>>>>each line.
>>>>>>
>>>>>The total number of lines may vary anywhere from one to as
>>>>>much as 8 lines of data.
>>>>>>
>>>>>The line should look like this when pieced together;
>>>>>>
>>>>>2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
>>>>>404.2.01
>>>>>(6511)
>>>>>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48
>>>>>52
>>>>>00
>>>>>52
>>>>>02 02
>>>>>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06
>>>>>06
>>>>>06
>>>>>06
>>>>>06 0A 06
>>>>>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06
>>>>>06
>>>>>06
>>>>>06
>>>>>06
>>>>>06 06 06 40
>>>>>All in one big line without word wrap.
>>>>>The next entry would be a different header (ARES_INDICATION
>>>>>for
>>>>>example).
>>>>>"Jesse Houwing" wrote:
>>>>>>Hello Brian,
>>>>>>>
>>>>>>>2007/07/27 11:00:03 [153006]
>>>>>>>>
>>>>>>My guess is that these aren't the same length for every
>>>>>>line. If they were it would be easy:
Aug 8 '07 #16

P: n/a
Hello Brian,
Jesse I thought I would let you know how I did the highlight/change
color of specific text;
Great, seems like you're getting the hang of it :)

As an exercise you could try convirting it all to reular expressions for
fun ;)

Jesse

>
private void findSequenceNumbers()
{
toolStripStatusLabel.Visible =
toolStripProgressBar.Visible =
true;
toolStripProgressBar.Value = 0;
int lineNum = 0;
bool startingNewLine = true;
FontStyle style = FontStyle.Bold;
string[] lines = rtbTextBox.Lines;
string text = rtbTextBox.Text;
toolStripProgressBar.Maximum = text.Length;
for (int i = 0; i < text.Length; i++)
{
if (startingNewLine)
{
if ((lines[lineNum].Contains("ARES_EINDICATION"))
||
(lines[lineNum].Contains("ARES_INDICATION")))
{
i += 169;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^
style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else if
(lines[lineNum].Contains("]CODELINE_INDICATION_MSG"))
{
i += 160;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^
style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else
{
i += lines[lineNum].Length - 1;
}
startingNewLine = false;
Application.DoEvents();
}
if (text[i] == '\n')
{
startingNewLine = true;
lineNum++;
}
toolStripProgressBar.Value = i;
}
toolStripStatusLabel.Visible =
toolStripProgressBar.Visible =
false;
}
Many Thanks,

Brian

"Jesse Houwing" wrote:
>Hello Brian,
>>Jesse, I appreciate all your help so far, and have now come up with
something I am once again unsure how to fix.;

In the Example.txt file I sent you, when the lines are combined into
one they are a little jumbled. I would like to format the line so
that the RX and TX portion all start at the 78th position from the
left. Everything to the left of that would never go past the 75th
position.

I know that I can perform a StringBuilder.Insert, just not sure how
or where to add it into this code.

Here is the code for what we made work.
Basically you have two options.
1) Find the location and insert as many spaces as needed.
2) Find the location, split the string. Now use
stringBuilder.AppendFormat
function
if you use the following format: "{0, -78}{1}" and the two parts of
the string
you have collected using substring it will padd the string to 78
positions
if needed:
/* might need more specific searching for RX/TX */
if (line.InfexOf("TX") 0 || line.InfexOf("RX") 0)
{
int charactersTillPoint = /* Find out the position */
string part0 = line.SubString(0, charactersTillPoint);
string part1 = line.SubString(charactersTillPoint);
sb.AppendFormat("{0, -78}{1}", part0, part1);
}
I've inserted where to mark it with #####
>>---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
sb.Append("\r\n");
#### This is where to insert
>>sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---
Thanks,

"Jesse Houwing" wrote:

Hello Brian,

I have no clue how to give text a different colour. I am sure
though that you would need to use a RichTextBox. I'd guess you'd
have to insert some RTF control characters. Where to insert them
would be easy to find out if it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}
It's all in one function now, but I'd place the extra checks in a
subfunction should they grow any further.
I tried to find a string function that automatically gives the xth
position of a substring (IndexOf(what, from, count, number) but I
couldn't find it... too bad ;)).

Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexp air>[A-F0-9]{2})",
"$1" + "control character for colour" + "$2" + "control character
for normal colour");

It's actually easier for me to read than the function above, but
that is partly due to the fact that I'm presenting courses in .NET
Regular Expressions as part of my job.

Jesse

I wnt with the Stringbuilder solution. It seemed easier to follow
to me than the Regex one.
>
"Jesse Houwing" wrote:
>
>Hello Brian,
>>
>Did you go for the regex or the stringbuilder solution?
>>
>Jesse
>>
>>Ok, Jessee, I have a new Wrinkle for you.
>>>
>>Lets say I want a specific hex pair highlighted in Blue on every
>>line that starts out with ARES_INDICATION or ARES_EINDICATION.
>>>
>>It would be the 34 item from the left.
>>>
>>If you have the Example.txt I emailed you, the last line in the
>>how I want it to look the field would the C7 that follows the 02
>>02 hex.
>>>
>>If not I can send it to you again.
>>>
>>"Jesse Houwing" wrote:
>>>
>>>Hello Brian,
>>>>
>>>>Thank you Jesse, that will help a bunch. I have C# classes
>>>>scheduled Aug/Sep/Oct so that I can try to avoid more
>>>>questions in the future.
>>>>>
>>>You're very welcome and don't hesitate to ask questions that is
>>>what these newsgroups are for after all. Just make sure you've
>>>tried ;)
>>>>
>>>Jesse
>>>>
>>>>V/R
>>>>>
>>>>Brian
>>>>>
>>>>"Jesse Houwing" wrote:
>>>>>
>>>>>Hello Brian,
>>>>>>
>>>>>You can do this quite easily with either a regex or a simple
>>>>>function I'll try to demonstrate both:
>>>>>>
>>>>>basically you need a regex that matches the
>>>>>\r\ndate+time+[number] that is followed by two spaces and
>>>>>replace it with a space
>>>>>>
>>>>>so the regex looks like this (I simplified it a bit, but
>>>>>given the fact that all the lines are very well layed out I
>>>>>don't thing you have to make it more differcult :).
>>>>>>
>>>>>\r\n[^\]]+\][ ]{2}
>>>>>>
>>>>>now replace this by " " and you're all set.
>>>>>>
>>>>>So:
>>>>>private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
>>>>>RegexOptions.Compiled);
>>>>>private string LayoutInput(string input)
>>>>>{
>>>>>return _rx.Replace(input, " ");
>>>>>}
>>>>>Or you could use a StringReader/StringBuilder combination:
>>>>>private string LayoutInput(string input)
>>>>>{
>>>>>StringReader sr = new StringReader(input);
>>>>>StringBuilder sb = new StringBuilder(input.Length);
>>>>>string line;
>>>>>while ((line = sr.ReadLine())!= null)
>>>>>{
>>>>>if (line.Length < 29) { throw new
>>>>>InvalidOperationException("invalid
>>>>>input"); /* or simply continue; */ }
>>>>>if (line[29] != ' ')
>>>>>{
>>>>>if (sb.Length 0)
>>>>>{
>>>>>sb.Append("\r\n");
>>>>>}
>>>>>sb.Append(line);
>>>>>}
>>>>>else
>>>>>{
>>>>>sb.Append(line.Substring(30));
>>>>>}
>>>>>}
>>>>>return sb.ToString();
>>>>>}
>>>>>I haven't used much error checking to make sure the format is
>>>>>correct. But I think you can go on from here.
>>>>>Jesse
>>>>>>Sent you an email with a sampling.
>>>>>>>
>>>>>>"Jesse Houwing" wrote:
>>>>>>>
>>>>>>>Hello Brian,
>>>>>>>>
>>>>>>>Could you send a sample file with two of these data blocks
>>>>>>>f what they should look like and how you receive them
>>>>>>>directly to my email? I'll reply back to this thread with a
>>>>>>>solution when I have time to go over it. My newsreader
>>>>>>>keeps messing up your samples, so it would be easier if I
>>>>>>>had two text files.
>>>>>>>>
>>>>>>>Jesse
>>>>>>>>
>>>>>>>>Jesse the data inside of the [] is static six digits for
>>>>>>>>each line.
>>>>>>>>>
>>>>>>>>The total number of lines may vary anywhere from one to as
>>>>>>>>much as 8 lines of data.
>>>>>>>>>
>>>>>>>>The line should look like this when pieced together;
>>>>>>>>>
>>>>>>>>2007/07/27 11:00:03 [153006]ARES_INDICATION
>>>>>>>>010.050.016.002
>>>>>>>>404.2.01
>>>>>>>>(6511)
>>>>>>>>RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13
>>>>>>>>48
>>>>>>>>52
>>>>>>>>00
>>>>>>>>52
>>>>>>>>02 02
>>>>>>>>C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06
>>>>>>>>06
>>>>>>>>06
>>>>>>>>06
>>>>>>>>06
>>>>>>>>06 0A 06
>>>>>>>>06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06
>>>>>>>>06
>>>>>>>>06
>>>>>>>>06
>>>>>>>>06
>>>>>>>>06 06 06 40
>>>>>>>>All in one big line without word wrap.
>>>>>>>>The next entry would be a different header
>>>>>>>>(ARES_INDICATION
>>>>>>>>for
>>>>>>>>example).
>>>>>>>>"Jesse Houwing" wrote:
>>>>>>>>>Hello Brian,
>>>>>>>>>>
>>>>>>>>>>2007/07/27 11:00:03 [153006]
>>>>>>>>>>>
>>>>>>>>>My guess is that these aren't the same length for every
>>>>>>>>>line. If they were it would be easy:
>>>>>>>>>>

Aug 8 '07 #17

P: n/a
Maybe after I go thru a class the end of the month.

Thanks for all your help.

"Jesse Houwing" wrote:
Hello Brian,
Jesse I thought I would let you know how I did the highlight/change
color of specific text;

Great, seems like you're getting the hang of it :)

As an exercise you could try convirting it all to reular expressions for
fun ;)

Jesse


private void findSequenceNumbers()
{
toolStripStatusLabel.Visible =
toolStripProgressBar.Visible =
true;
toolStripProgressBar.Value = 0;
int lineNum = 0;
bool startingNewLine = true;
FontStyle style = FontStyle.Bold;
string[] lines = rtbTextBox.Lines;
string text = rtbTextBox.Text;
toolStripProgressBar.Maximum = text.Length;
for (int i = 0; i < text.Length; i++)
{
if (startingNewLine)
{
if ((lines[lineNum].Contains("ARES_EINDICATION"))
||
(lines[lineNum].Contains("ARES_INDICATION")))
{
i += 169;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^
style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else if
(lines[lineNum].Contains("]CODELINE_INDICATION_MSG"))
{
i += 160;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^
style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else
{
i += lines[lineNum].Length - 1;
}
startingNewLine = false;
Application.DoEvents();
}
if (text[i] == '\n')
{
startingNewLine = true;
lineNum++;
}
toolStripProgressBar.Value = i;
}
toolStripStatusLabel.Visible =
toolStripProgressBar.Visible =
false;
}
Many Thanks,

Brian

"Jesse Houwing" wrote:
Hello Brian,

Jesse, I appreciate all your help so far, and have now come up with
something I am once again unsure how to fix.;

In the Example.txt file I sent you, when the lines are combined into
one they are a little jumbled. I would like to format the line so
that the RX and TX portion all start at the 78th position from the
left. Everything to the left of that would never go past the 75th
position.

I know that I can perform a StringBuilder.Insert, just not sure how
or where to add it into this code.

Here is the code for what we made work.

Basically you have two options.
1) Find the location and insert as many spaces as needed.
2) Find the location, split the string. Now use
stringBuilder.AppendFormat
function
if you use the following format: "{0, -78}{1}" and the two parts of
the string
you have collected using substring it will padd the string to 78
positions
if needed:
/* might need more specific searching for RX/TX */
if (line.InfexOf("TX") 0 || line.InfexOf("RX") 0)
{
int charactersTillPoint = /* Find out the position */
string part0 = line.SubString(0, charactersTillPoint);
string part1 = line.SubString(charactersTillPoint);
sb.AppendFormat("{0, -78}{1}", part0, part1);
}
I've inserted where to mark it with #####

---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
sb.Append("\r\n");
#### This is where to insert

sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---
Thanks,

"Jesse Houwing" wrote:

Hello Brian,

I have no clue how to give text a different colour. I am sure
though that you would need to use a RichTextBox. I'd guess you'd
have to insert some RTF control characters. Where to insert them
would be easy to find out if it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}
It's all in one function now, but I'd place the extra checks in a
subfunction should they grow any further.
I tried to find a string function that automatically gives the xth
position of a substring (IndexOf(what, from, count, number) but I
couldn't find it... too bad ;)).

Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpa ir>[A-F0-9]{2})",
"$1" + "control character for colour" + "$2" + "control character
for normal colour");

It's actually easier for me to read than the function above, but
that is partly due to the fact that I'm presenting courses in .NET
Regular Expressions as part of my job.

Jesse

I wnt with the Stringbuilder solution. It seemed easier to follow
to me than the Regex one.

"Jesse Houwing" wrote:

Hello Brian,
>
Did you go for the regex or the stringbuilder solution?
>
Jesse
>
>Ok, Jessee, I have a new Wrinkle for you.
>>
>Lets say I want a specific hex pair highlighted in Blue on every
>line that starts out with ARES_INDICATION or ARES_EINDICATION.
>>
>It would be the 34 item from the left.
>>
>If you have the Example.txt I emailed you, the last line in the
>how I want it to look the field would the C7 that follows the 02
>02 hex.
>>
>If not I can send it to you again.
>>
>"Jesse Houwing" wrote:
>>
>>Hello Brian,
>>>
>>>Thank you Jesse, that will help a bunch. I have C# classes
>>>scheduled Aug/Sep/Oct so that I can try to avoid more
>>>questions in the future.
>>>>
>>You're very welcome and don't hesitate to ask questions that is
>>what these newsgroups are for after all. Just make sure you've
>>tried ;)
>>>
>>Jesse
>>>
>>>V/R
>>>>
>>>Brian
>>>>
>>>"Jesse Houwing" wrote:
>>>>
>>>>Hello Brian,
>>>>>
>>>>You can do this quite easily with either a regex or a simple
>>>>function I'll try to demonstrate both:
>>>>>
>>>>basically you need a regex that matches the
>>>>\r\ndate+time+[number] that is followed by two spaces and
>>>>replace it with a space
>>>>>
>>>>so the regex looks like this (I simplified it a bit, but
>>>>given the fact that all the lines are very well layed out I
>>>>don't thing you have to make it more differcult :).
>>>>>
>>>>\r\n[^\]]+\][ ]{2}
>>>>>
>>>>now replace this by " " and you're all set.
>>>>>
>>>>So:
>>>>private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
>>>>RegexOptions.Compiled);
>>>>private string LayoutInput(string input)
>>>>{
>>>>return _rx.Replace(input, " ");
>>>>}
>>>>Or you could use a StringReader/StringBuilder combination:
>>>>private string LayoutInput(string input)
>>>>{
>>>>StringReader sr = new StringReader(input);
>>>>StringBuilder sb = new StringBuilder(input.Length);
>>>>string line;
>>>>while ((line = sr.ReadLine())!= null)
>>>>{
>>>>if (line.Length < 29) { throw new
Aug 8 '07 #18

P: n/a
Brian Cook wrote:
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
Spot the bug.

BTW: I wouldn't throw that exception. Here is its definition: " The
exception that is thrown when a method call is invalid for the object's
current state.".

Hilton
Oct 4 '07 #19

P: n/a
Thanks,

"Hilton" wrote:
Brian Cook wrote:
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')

Spot the bug.

BTW: I wouldn't throw that exception. Here is its definition: " The
exception that is thrown when a method call is invalid for the object's
current state.".

Hilton
Oct 4 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.