C# regular expression multi rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sandason
    New Member
    • Feb 2008
    • 3

    C# regular expression multi rows

    I have a multiple row string that i want to match from somewhere in the string to the first occurance of another word.

    String = "Error Message: This is an error message and the error code for this is 443.
    Error_ID = 999-Affd
    Solution: Check mainTable 33."


    So i want to grab "Error Message:" as the keyword to find then include all the text up to the word Solution
    Something like:
    Regex re = new Regex(@"Error Message:.*\n ??????", RegexOptions.Ig noreCase);
    not sure what to put in for the ??????
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Since you know that the string has to start with "Error Message"

    you coud check it like

    Code:
    string theMessage ="";
    If (errorString.StartsWith("Error Message") == true)
    {
       theMessage = errorString.Replace("Error Message :","");
    }
    if you do want to use regular expression Im not sure how the regex would look like but will have to look something like

    "$[E]{1}[r]{2}[o]{1}[r]{1}[ ]{1}[M]{1}[e]{1}[s]{2}[a]{1}[g]{1}[e]{1}"

    but im not sure about it! (regular expression is not anywhere near my strong or weak point :P )

    Comment

    • Sandason
      New Member
      • Feb 2008
      • 3

      #3
      Thanks for your input, i already have the ability to grab all instances of "error message" and everything on the same line after it up to a new line but nothing that will grab everything afterwards with say 1 or 2 new lines ending with the word "solution" this is where my issue is.

      Comment

      • Sandason
        New Member
        • Feb 2008
        • 3

        #4
        String = "Error Message: This is an error message and the error code for this is 443.
        Error_ID = 999-Affd
        Solution: Check mainTable 33."

        I think i found my solution
        Regex re2 = new Regex(@"Error Message:.*?Solu tion", RegexOptions.Si ngleline);

        Comment

        Working...