Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:04:03+00:00 2026-06-15T13:04:03+00:00

Apologies if this is a duplicate; I promise I did search. I’m working on

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:04:05+00:00Added an answer on June 15, 2026 at 1:04 pm

    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.");    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this is somewhat of a duplicate question, I did see others but
Apologies if this is a duplicate - I did a few searches and didn't
Apologies if this is a duplicate, 20 minutes of searching didn't yield this exact
My apologies if this is a duplicate; I may not know the proper terms
Apologies if this seems like a duplicate post... Thomas Warner kindly answeres an earlier
Apologies if this is a repost, I could not find the search terms to
As the title says, I'm using SQL Server 2008. Apologies if this question is
Apologies, this is a duplicate of entry of this post but I found I'm
Apologies if this is a duplicate, but I've not managed to find this question
apologies if this is a duplicate. Is there any way to determine what generic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.