We help IT Professionals succeed at work.

Using Regex.Escape

tnova1
tnova1 asked
on
Medium Priority
239 Views
Last Modified: 2010-04-16
Hi,

I got these two lines of code:

        1. text = text.Replace("\", Regex.Escape("\"));
        2. text = text.Replace("'", "\'");

The first line give me a compiling error:
         error CS1010: Newline in constant

Rem out the first line.  Run w/ the debugger the second line did not work, it didnot replace ' with \'

Please advise.

Diane Ha

Comment
Watch Question

Commented:
2. should be

text = text.Replace("'", "\\'");

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
1. should be

text = Regex.Replace(text, Regex.Escape("\\"));

Author

Commented:
Hi hongjun

    The correct answer for the first line:
          text.Replace("\\", Regex.Escape("\\"));

     Your 2nd line is right.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.