Using Regex.Escape

Posted on 2006-03-28
Last Modified: 2010-04-16

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

Question by:tnova1
    LVL 33

    Accepted Solution

    2. should be

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

    Expert Comment

    1. should be

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

    Author Comment

    Hi hongjun

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

         Your 2nd line is right.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now