Why doesn't this regex pattern match this text? (Simple regex just includes escaped text with wildcard in the middle)

StackOverflow https://stackoverflow.com/questions/13689290

Question

Apologies if this is a duplicate; I promise I did search.

I'm working on a C# integration testing project using NUnit in Visual Studio 2010 Professional, running the tests with Resharper v6.1. (Resharper 6.1 dictates that I use NUnit 2.5.10; I've always had trouble trying to override that.)

Part of my testing is to assert that the proper exceptions are thrown, including the exception messages. However, sometimes the exception message includes a guid or other information that I don't expect to be the exact same every time. To get around this, I want to match the exception message using a basic regex pattern where I include the exception message and simply use a wildcard where in certain spots to say "this part can match anything" I've tried to do this in the scenario below, but every time I get an error that there is no match. Summary of the code:

[Test]
public void ExceptionPatternTest()
{
var patternA = Regex.Escape(@"SQL Code: 2601 Message: Cannot insert duplicate key row in object 'dbo.ResourceCategory' with unique index 'IDX_ResourceTypeResourceCategoryName'.");
var patternB = ".*";  //wildcard
var patternC = Regex.Escape(@"\r\nThe statement has been terminated.");    
var pattern = patternA + patternB + patternC;

var thrownException = Assert.Throws(Is.TypeOf(_testException.GetType()),
               () => Throw()); //Call method and assert that exception is thrown.

Assert.That(Regex.IsMatch(thrownException.Message, pattern));
}

private void Throw()
{
    throw _testException;
}

readonly Exception _testException = new Exception("SQL Code: 2601 Message: Cannot insert duplicate key row in object 'dbo.ResourceCategory' with unique index 'IDX_ResourceTypeResourceCategoryName'. The duplicate key value is (9fec90c1-12b4-42c3-adc0-a0d600b9a8e8, 1, Item Cat 1).\r\nThe statement has been terminated.");

I've also tried variations on parts of this. Including a manual escaping of characters for the pattern:

var pattern = @"SQL\ Code:\ 2601\ Message:\ Cannot\ insert\ duplicate\ key\ row\ in\ object\ 'dbo\.ResourceCategory'\ with\ unique\ index\ 'IDX_ResourceTypeResourceCategoryName'\..*\\r\\nThe\ statement\ has\ been\ terminated\.";

And I've tried StringAssert too, in case there was a difference:

StringAssert.IsMatch(pattern, thrownException.Message);

Thanks in advance for your help! Also, apologies if I'm just missing something really dumb. I've only worked with regex sparingly, so that is certainly possible.

Was it helpful?

Solution

Regex.Escape(@"\r\n") will produce \\r\\n, which is not what you want. I would suggest moving the newline to the unescaped string instead:

var patternB = @".*\r\n";
var patternC = Regex.Escape(@"The statement has been terminated.");    

OTHER TIPS

Why use a complicated regex when you can just use string.StartsWith and string.EndsWith to check the prefix and suffix? Less code, easier to read, and no messing about trying to work out why a seemingly simple thing doesn't work right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top