Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex question

Status
Not open for further replies.

carlgomerski

Programmer
Jan 20, 2001
17
0
0
AU
Hi,

I'm very new to regex and I'm trying to cleanup a string (which is xml) being passed into a web service. The string/xml's getting extra '/' values added. Via a regex replace I'm trying to change:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
to:
<?xml version="1.0" encoding="UTF-8"?>

I'm using '@"<?xml version=\""1.0\"" encoding=\""UTF-8\""\?>"' which seems to find the value? For the life of me I can't figure out the regex expression in the replace?
 
If all you want to do is remove the "\" character, then use String.Replace - RegEx is overkill.


Hope this helps.

[vampire][bat]
 
Let us suppose for a mad minute that you did actually want to do this with a Regular Expression, the match you want is @"\\" and the replace would be an empty string. Like this:

Code:
string result = Regex.Replace(testString, @"\\", "");

But as earthandfire said this will be so much faster if you use String.Replace as it won't have to load the whole Regex engine into memory.

Code:
string result = testString.Replace( @"\", "" );
 
The string/xml's getting extra '/' values added
Rather than stripping out the unwanted characters, can't you fix the code that's putting them in there in the first place? It looks like the original string is being incorrectly escaped when it is set up...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
New to C# and Regex so bare with me...

My problem concerns the way C# handles strings (in a way I don't understand, I come from a VB background).

The String.Replace didn't work.

I'm passing in a value into a string variable, when it hits a double quote character it wants to put in a '\' character.

For example the C# code:
string xx = @"<aaa bb=""C"" />";

When I put a break point in and look at variable 'xx' in the IDE it returns '<aaa bb=\"C\" />', adding the '\' character. When i pass the string into a xmlDocument the '\' characters are still there which invalidates my XML.

My inexpierence with C# and the way it handles strings is most likely a factor here... The regex and string replace dont' or won't get rid the '\' characters?

 
In that case you don't need to do anything!

The Debugger is placing the \ before the " to show that it is not the end of the string. The \ does not exist in the string in memory.

In your example you are using a VB style string constant which is what the @ infront of it means. In C# the normal way to write string constants allows you to add special characters using a \ character so:

string xx = @"<aaa bb=""C"" />";

is exactly the same as

string xx = "<aaa bb=\"C\" />";

You can also add charage returns and line feeds using this method.

string s = "Line 1\r\nLine2";

is equivalent to this VB.Net code:

Dim s As String = "Line1" + vbCrLf + "Line2"

To insert a slash you just double it so:

string s = "12 \\ 13";

is equivalent to this VB.Net code:

Dim s As String = "12 \ 13
 
The String.Replace didn't work.

Did you use yourstring = yourstring.Replace("\\", "");

or did you just do yourstring.Replace("\\", ""); without setting it back to the variable itself? The replace method returns a string - not modify the existing one directly...
 
Thanks guys for the great responses.

Jurkmonkey...tried the string.replace as you suggested but the '\'s are still there. I think Aptitude's on the right track with regards to the added '\' being the way C# handles string variables with double quote characters in them.

The reason I noticed the '\' values was because I load the string into an xmlDocument and was having trouble with 'selectnodes', 'selectsinglenode' commands not returning data which I thought was because the xml might be considered invalid with the '\' characters in it? Might be ok and my xpath statements not set up correctly.

Will continue on...thanks
 
What is in your XML document and what XPath are you using? May be we can help with that too?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top