Unrecognized escape sequence

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Ireland

    Unrecognized escape sequence

    Hi --

    Any ideas on this one please ...

    The following code works as expected:
    private void Form1_Load(obje ct sender, System.EventArg s e) {
    string text = "The quick brown"+ " FOX " +"jumps over the lazy dog.";
    string pattern = "FOX";
    string replacement = "cat";

    string
    result=Regex.Re place(text,patt ern,replacement ,System.Text.Re gularExpression s
    ..RegexOptions. IgnoreCase);
    richTextBox1.Te xt = result;
    }

    However, this code doesn't:
    private void Form1_Load(obje ct sender, System.EventArg s e) {
    string text = "The quick brown"+ " C:\\TEST\\MyDir ectory " +"jumps over the
    lazy dog.";
    string pattern = "C:\\TEST\\MyDi rectory";
    string replacement = "..\\..";

    string
    result=Regex.Re place(text,patt ern,replacement ,System.Text.Re gularExpression s
    ..RegexOptions. IgnoreCase);
    richTextBox1.Te xt = result;
    }

    The above bombs out with an Unrecognized escape sequence. All I'm trying to
    do is perform a case-insensitive replace function on text with directory
    paths in it ..

    Many thanks!

    Chris.


  • Niki Estner

    #2
    Re: Unrecognized escape sequence

    The problem is that regular expressions use the backslash as escape
    characters, too; Use Regex.Escape(pa ttern).
    This will escape all regex-metacharacters in the pattern string.

    Niki

    "Christophe r Ireland" <dead@asadoorna il.com> wrote in
    news:ORx6D$XnEH A.3460@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi --
    >
    > Any ideas on this one please ...
    >
    > The following code works as expected:
    > private void Form1_Load(obje ct sender, System.EventArg s e) {
    > string text = "The quick brown"+ " FOX " +"jumps over the lazy dog.";
    > string pattern = "FOX";
    > string replacement = "cat";
    >
    > string
    > result=Regex.Re place(text,patt ern,replacement ,System.Text.Re gularExpression s
    > .RegexOptions.I gnoreCase);
    > richTextBox1.Te xt = result;
    > }
    >
    > However, this code doesn't:
    > private void Form1_Load(obje ct sender, System.EventArg s e) {
    > string text = "The quick brown"+ " C:\\TEST\\MyDir ectory " +"jumps over
    > the
    > lazy dog.";
    > string pattern = "C:\\TEST\\MyDi rectory";
    > string replacement = "..\\..";
    >
    > string
    > result=Regex.Re place(text,patt ern,replacement ,System.Text.Re gularExpression s
    > .RegexOptions.I gnoreCase);
    > richTextBox1.Te xt = result;
    > }
    >
    > The above bombs out with an Unrecognized escape sequence. All I'm trying
    > to
    > do is perform a case-insensitive replace function on text with directory
    > paths in it ..
    >
    > Many thanks!
    >
    > Chris.
    >
    >[/color]


    Comment

    • Christopher Ireland

      #3
      Re: Unrecognized escape sequence

      "Niki Estner" <niki.estner@cu be.net> wrote in message
      news:uVHEkPbnEH A.608@TK2MSFTNG P09.phx.gbl...[color=blue]
      > The problem is that regular expressions use the backslash as escape
      > characters, too; Use Regex.Escape(pa ttern).
      > This will escape all regex-metacharacters in the pattern string.[/color]

      Excellent! Thanks Niki!!

      Chris.


      Comment

      Working...