regex replace pipe character

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

}
}
}
 
J

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
 
G

GlennH

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

Glenn
 
J

Jon Shemitz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top