To begin, I would like to note that a similar question exists with answers and workarounds specific to PHP. I am seeing this issue in C# and I would like to understand the logic behind this apparent "gotcha".
The word boundary character \b
doesn't seem to work properly when placed inside a Regex
set (aka "box brackets": []
). Is this a syntactic issue, are word boundaries intentionally excluded from set matching, or is there some other explanation I'm missing?
Here is a program to demonstrate the issue:
namespace TestProgram
{
using System.Text.RegularExpressions;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
var text = "[abc]";
var BaselineRegex = new Regex(@"(?:\b)(abc)");
Debug.Assert(BaselineRegex.IsMatch(text)); // Assertion Passes
var BracketRegex = new Regex(@"(?:[\b])(abc)");
Debug.Assert(BracketRegex.IsMatch(text)); // Assertion Fails!
}
}
}
Here are web versions to demonstrate as well: