none
Search in a string RRS feed

  • Question

  • I have a string error from SQL. I need to search the string for:

    "Transaction (Process ID 52) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction."

    But it could be anything between (???)

    Like strings.Contains("Transaction (Process ID 52) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.");

    But Process ID 52 can be anything


    www.helixpoint.com

    Thursday, September 5, 2019 5:04 PM

Answers

  • var pattern = @"^Transaction \(Process ID \d+\) was deadlocked$";
    
    var regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
    
    var input = "Transaction (Process ID 52) was deadlocked";
    var isMatch = regex.IsMatch(input);


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Helixpoint Friday, September 6, 2019 11:03 AM
    Thursday, September 5, 2019 7:27 PM
    Moderator

All replies

  • If you want to use `Contains` then just include everything up to the flexible value.

    string.Contains("Transaction (Process ID");
    If you are getting related errors and you only care about deadlocks then just search for the deadlocks keyword.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, September 5, 2019 5:40 PM
    Moderator
  • No. I need to make sure the whole string in in there, but the process id may be different. That is my issue

    www.helixpoint.com

    Thursday, September 5, 2019 5:53 PM
  • Why? How many different errors are you expecting from SQL that would have the bulk of the string you're searching for in there? If you search on the bulk of the string then the odds of getting a false positive are pretty much zero. 

    However if you really want to exactly match a string then you're going to have to use Regex instead. Put that entire string into a Regex. Replace the number with \d+ to match any number and then enable options to not enforce case. It'll be slower and harder to read compared to Contains though.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, September 5, 2019 6:03 PM
    Moderator
  • Not sure how to do that? Any help would be appreciated 

    www.helixpoint.com

    Thursday, September 5, 2019 6:28 PM
  • var pattern = @"^Transaction \(Process ID \d+\) was deadlocked$";
    
    var regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
    
    var input = "Transaction (Process ID 52) was deadlocked";
    var isMatch = regex.IsMatch(input);


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Helixpoint Friday, September 6, 2019 11:03 AM
    Thursday, September 5, 2019 7:27 PM
    Moderator
  • Is it really that hard to search for "Transaction (Process ID" and then search for ") was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction" ?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, September 6, 2019 12:36 AM
  • Don't try to parse error string to determine the type of error. SqlException have error code value that works even if user have different language version of runtime installed.

    SqlException.Number is your friend.

    Friday, September 6, 2019 1:47 AM
    Answerer
  • Ya. If I could I would

    www.helixpoint.com

    Friday, September 6, 2019 11:03 AM