Replacing escapsequences with characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christof Nordiek

    Replacing escapsequences with characters

    Hi,

    i have to interpret a string in wich escape sequences are used to escape
    special characters. How can I replace these sequences by the character they
    replace?

    Escapint rules are:

    \ -\\
    : -\:
    " -\"

    so the characters \ : and " are escaped by \

    I figured out a solution, but it fails where a douled backslash is followed
    by another escape sequence.

    Thanks

    Christof

  • Marc Gravell

    #2
    Re: Replacing escapsequences with characters

    How about:

    public static string Escape(string value)
    {
    return re.Replace(valu e, @"\$1");
    }
    static readonly Regex re = new Regex(@"([\\:\""])",
    RegexOptions.Co mpiled);

    ?

    Marc

    Comment

    • Christof Nordiek

      #3
      Re: Replacing escapsequences with characters

      "Marc Gravell" <marc.gravell@g mail.comschrieb im Newsbeitrag
      news:f8b42103-43ef-4afa-a1df-a8013c3bf2c8@s1 3g2000prd.googl egroups.com...
      How about:
      >
      public static string Escape(string value)
      {
      return re.Replace(valu e, @"\$1");
      }
      static readonly Regex re = new Regex(@"([\\:\""])",
      RegexOptions.Co mpiled);
      >
      ?
      >
      Marc
      Thanks Marc,

      but what i'm actually searching is unescaping not escaping.

      Christof

      Comment

      • Marc Gravell

        #4
        Re: Replacing escapsequences with characters

        Fine - sorry, I thought your rule table was the forwards direction; how
        about:

        public static string Unescape(string value)
        {
        return re.Replace(valu e, @"$2");
        }
        static readonly Regex re = new Regex(@"(\\)([\\:""])",
        RegexOptions.Co mpiled);

        Haven't tested it, though...


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Replacing escapsequences with characters

          Christof,

          I can't help but think of state processing here. Basically, cycle
          through every character in the string. If you come across a slash character
          "\", then you put yourself in an "escape sequence" state. The loop
          continues, and you process the character according to the state you are in,
          and revert the state back (so the loop can process normally) when you have
          finished processing the escape sequence.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Christof Nordiek" <cn@nospam.dewr ote in message
          news:%23ahG2%23 jbIHA.3572@TK2M SFTNGP02.phx.gb l...
          Hi,
          >
          i have to interpret a string in wich escape sequences are used to escape
          special characters. How can I replace these sequences by the character
          they replace?
          >
          Escapint rules are:
          >
          \ -\\
          : -\:
          " -\"
          >
          so the characters \ : and " are escaped by \
          >
          I figured out a solution, but it fails where a douled backslash is
          followed by another escape sequence.
          >
          Thanks
          >
          Christof

          Comment

          • Luc E. Mistiaen

            #6
            Re: Replacing escapsequences with characters

            This maybe...

            public static string UnEscape(string value)
            {
            return re.Replace(valu e, @"$1");
            }
            static readonly Regex re = new Regex(@"\\(.)",
            RegexOptions.Co mpiled);


            "Christof Nordiek" <cn@nospam.dewr ote in message
            news:OotokqlbIH A.5984@TK2MSFTN GP06.phx.gbl...
            "Marc Gravell" <marc.gravell@g mail.comschrieb im Newsbeitrag
            news:f8b42103-43ef-4afa-a1df-a8013c3bf2c8@s1 3g2000prd.googl egroups.com...
            >How about:
            >>
            > public static string Escape(string value)
            > {
            > return re.Replace(valu e, @"\$1");
            > }
            > static readonly Regex re = new Regex(@"([\\:\""])",
            >RegexOptions.C ompiled);
            >>
            >?
            >>
            >Marc
            >
            Thanks Marc,
            >
            but what i'm actually searching is unescaping not escaping.
            >
            Christof

            Comment

            • Christof Nordiek

              #7
              Re: Replacing escapsequences with characters

              "Marc Gravell" <marc.gravell@g mail.comschrieb im Newsbeitrag
              news:uUNqHulbIH A.4752@TK2MSFTN GP02.phx.gbl...
              Fine - sorry, I thought your rule table was the forwards direction; how
              about:
              >
              public static string Unescape(string value)
              {
              return re.Replace(valu e, @"$2");
              }
              static readonly Regex re = new Regex(@"(\\)([\\:""])",
              RegexOptions.Co mpiled);
              >
              Haven't tested it, though...
              Thank you, seems to work.
              What I still don't udnerstand is, why this expression doesn't match the 2.
              and 3. char of @"\\\:".

              Christof

              Comment

              • Marc Gravell

                #8
                Re: Replacing escapsequences with characters

                As I understand regex (I'm not an expert) a match is then excluded - i.e. it
                matches the first 2 characters, does a replace and keeps looking from the
                (orignal) 3rd character.

                It gets more confusing if you use greedy matches though...

                Marc


                Comment

                Working...