Slee
Slee

Reputation: 28248

RegEx to remove Hexdecimal charecters from string in C#

There are some hexadecimal values hidden in some of my DB values that are causing me some trouble, does anyone have a RegEx to clean these out?

Here is the error i am getting:

'', hexadecimal value 0x19, is an invalid character.

This post explains my problem completely but I am looking for a RegEx solution: http://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/

Upvotes: 0

Views: 2429

Answers (4)

mtazva
mtazva

Reputation: 1015

I know you're hoping for Regex, but have you tried System.Security.SecurityElement.Escape? I believe this method is specifically intended for your use case.

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20620

Do you need something like....

    Regex regex = new Regex("0x\\d\\d");

    String Input = "this0x34is my 0x45 test 0x11";

    string Result = regex.Replace(Input, " ");

Upvotes: 0

Sebastian Mach
Sebastian Mach

Reputation: 39089

I could, but it would also any occurence of dead, beef, bee, be, b, the dea in dear, any decimal digit, any a, b, c, d, e

...

I oul, ut it woul lso ny ourn o , , , , , th in r, ny iml gt, ny , , , ,

...

See?

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283624

Instead of building XML strings by concatenation, you should use the XML handling classes provided with .NET. These will properly encode dangerous characters as XML entities and produce well-formed XML.

Also, regular expressions don't do well for XML processing.

Upvotes: 4

Related Questions