Conditional Search and Replace?

M

Mike Hildner

I'm receiving bytes over a socket and displaying in a text box. In the
responses I receive, some lines are delimited with x0A and others with
x0D0A. I'd like to search for x0A and replace with x0D0A, but only if x0A is
preceeded with x0D - so I don't end up with x0D0D0A.

Right now I'm searching for x0D0A, replacing with x0A, then searching for
x0A and replacing with x0D0A. Seems a little ugly.

Is there a better way to do this? I thought maybe regular expressions, but
don't know much about regex.

TIA,
Mike
 
S

solex

Mike,

Perhaps you can do some bit fiddling here.

As you read your stream look for the bit pattern (token) x0Dx0A using the
XOR statement

If xor yields zero then the bit patterns are identical.
Make your replacement in the output stream
move forward in your input stream the lenght of the token
Else
move forward one bit in your input stream
End If

Dan
 
J

Jay B. Harlow [MVP - Outlook]

Mike,
Is there a better way to do this? I thought maybe regular expressions, but
don't know much about regex.
This sounds like a good use of a RegEx

The following site seems to be a good intro to Regular-Expressions:

http://www.regular-expressions.info/

I'm sure there are others.

Unfortunately I do not use RegEx enough to give you a good working sample.

I believe you will need to use RegexOptions.Singleline to allow treating \r
& \n as characters.

Note: in RegEx: \r = x0D while \n = x0A.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top