regex replace pipe character

  • Thread starter Thread starter GlennH
  • Start date Start date
G

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","|",""));

}
}
}
 
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
 
Thanks Jon - its obvious I need the string literal AND the escape - now
you show me.

Glenn
 
GlennH said:
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.
 
Back
Top