Home All Groups Group Topic Archive Search About

regex replace pipe character

Author
4 Apr 2005 11:00 PM
GlennH
I am having trouble removing a pipe character using Regex.Replace -
see the 2 NUnit tests below:

The first replace works fine and the second Replace does not work.
I've tried escaping the pipe character.

Can anyone get this to work?

Thanks

Glenn



using System;
using NUnit.Framework;
using System.Text.RegularExpressions;

namespace ScratchPad
{
    [TestFixture] public class ScratchPad
    {
        [Test] public void RegexReplace_o()
        {
            Regex r = new Regex("bob");
            Match m = r.Match("bob");
            Assert.AreEqual(true,m.Success);
            Assert.AreEqual("bb",Regex.Replace("bob","o",""));
        }
        [Test] public void RegexReplace_bar()
        {
            Regex r = new Regex("b|b");
            Match m = r.Match("b|b");
            Assert.AreEqual(true,m.Success);
            //Assert.AreEqual("bb",Regex.Replace("b|b","|",""));

        }
    }
}

Author
5 Apr 2005 12:33 AM
Jon Shemitz
> I am having trouble removing a pipe character using Regex.Replace -
> see the 2 NUnit tests below:
>
> The first replace works fine and the second Replace does not work.
> I've tried escaping the pipe character.
>
> Can anyone get this to work?

> Regex.Replace("b|b","|",""));

Regex.Replace("b|b",@"\|","") // works for me

Are all your drivers up to date? click for free checkup

Author
5 Apr 2005 8:16 PM
GlennH
Thanks Jon - its obvious I need the string literal AND the escape - now
you show me.

Glenn
Author
5 Apr 2005 11:19 PM
Jon Shemitz
GlennH wrote:

> Thanks Jon - its obvious I need the string literal AND the escape - now
> you show me.

That interplay between regex escapes and string escapes seems to trip
up just about everyone at first.


Bookmark and Share