none
Best way yo split three consecutive phrases RRS feed

  • Question

  • Hi,

    I get a string like this :
    [DESCRIPTION:Expected GE,S1S,S1E,S3S,S3E,S3A,SVA,ST data segments under Functional Group context.][CODE:6039][DESCRIPTION:Transaction set/Message header missing.][CODE:14056][DESCRIPTION:Number of included transaction sets/messages 1 does not match actual count of 0][CODE:12417]

    And I would like it to be like this in multiline:
    [DESCRIPTION:Expected GE,S1S,S1E,S3S,S3E,S3A,SVA,ST data segments under Functional Group context.][CODE:6039]
    [DESCRIPTION:Transaction set/Message header missing.][CODE:14056]
    [DESCRIPTION:Number of included transaction sets/messages 1 does not match actual count of 0][CODE:12417]

    What would be the easiest way to accomplish this?
    Thank you
    Thursday, April 23, 2009 2:47 PM

Answers

  • Regular expressions would probably be the simplest way to accomplish this task.  The following code assumes "input" is the incoming string.

    MatchCollection matches = Regex.Matches(input, @"\[DESCRIPTION\:.+?\]\[CODE\:.+?\]");

     

    StringBuilder builder = new StringBuilder();

     

    foreach (Match match in matches)

        builder.AppendLine(match.Value);

     

    Console.WriteLine(builder.ToString());


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Guo Surfer Sunday, April 26, 2009 5:56 AM
    Thursday, April 23, 2009 3:00 PM
    Moderator
  • To build upon David's suggestion, a more advanced query using named match capture groups can be married with Linq to give on the ability to create dynamic entity sets which can be passed on to bindable controls. Here is an example:

    string input = "[DESCRIPTION:Expected GE,S1S,S1E,S3S,S3E,S3A,SVA,ST data segments under Functional Group context.][CODE:6039][DESCRIPTION:Transaction set/Message header missing.][CODE:14056][DESCRIPTION:Number of included transaction sets/messages 1 does not match actual count of 0][CODE:12417]";
    
    string pattern = 
    @"(?:\[[^:]+:)        # Match [DESCRIPTION]: but don't capture it (anchor)
      (?<Text>[^\]]+)     # What is the result; put it into a named capture group named Text.
      (?:\]\[CODE:)       # MBDC ][CODE(anchor)             
      (?<Code>[^\]]+)     # What is the code?
      (?:\])              # MBDC ";
    
    string[] mystring = new string[10];
    
    var items = from Match m in Regex.Matches( input, pattern, RegexOptions.IgnorePatternWhitespace )
                select new
                {
                    Text = m.Groups["Text"].Value,
                    Code = m.Groups["Code"].Value
                };
    
    foreach ( var result in items )
        Console.WriteLine( "{0,5} : {1}", result.Code, result.Text );
    
    /* outputs
     6039 : Expected GE,S1S,S1E,S3S,S3E,S3A,SVA,ST data segments under Functional Group context.
    14056 : Transaction set/Message header missing.
    12417 : Number of included transaction sets/messages 1 does not match actual count of 0             
     * /


    Note check out the regex forum to begin your journey into regex's Regular Expressions and check out the top level announcements to find resources such as programs to use to create and manipulate as well as tutorials.

    HTH
    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaManModerator Thursday, April 23, 2009 3:57 PM Better code formatting
    • Marked as answer by Guo Surfer Sunday, April 26, 2009 5:57 AM
    Thursday, April 23, 2009 3:55 PM
    Moderator

All replies

  • Regular expressions would probably be the simplest way to accomplish this task.  The following code assumes "input" is the incoming string.

    MatchCollection matches = Regex.Matches(input, @"\[DESCRIPTION\:.+?\]\[CODE\:.+?\]");

     

    StringBuilder builder = new StringBuilder();

     

    foreach (Match match in matches)

        builder.AppendLine(match.Value);

     

    Console.WriteLine(builder.ToString());


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Guo Surfer Sunday, April 26, 2009 5:56 AM
    Thursday, April 23, 2009 3:00 PM
    Moderator
  • Thanks
    Thursday, April 23, 2009 3:25 PM
  • To build upon David's suggestion, a more advanced query using named match capture groups can be married with Linq to give on the ability to create dynamic entity sets which can be passed on to bindable controls. Here is an example:

    string input = "[DESCRIPTION:Expected GE,S1S,S1E,S3S,S3E,S3A,SVA,ST data segments under Functional Group context.][CODE:6039][DESCRIPTION:Transaction set/Message header missing.][CODE:14056][DESCRIPTION:Number of included transaction sets/messages 1 does not match actual count of 0][CODE:12417]";
    
    string pattern = 
    @"(?:\[[^:]+:)        # Match [DESCRIPTION]: but don't capture it (anchor)
      (?<Text>[^\]]+)     # What is the result; put it into a named capture group named Text.
      (?:\]\[CODE:)       # MBDC ][CODE(anchor)             
      (?<Code>[^\]]+)     # What is the code?
      (?:\])              # MBDC ";
    
    string[] mystring = new string[10];
    
    var items = from Match m in Regex.Matches( input, pattern, RegexOptions.IgnorePatternWhitespace )
                select new
                {
                    Text = m.Groups["Text"].Value,
                    Code = m.Groups["Code"].Value
                };
    
    foreach ( var result in items )
        Console.WriteLine( "{0,5} : {1}", result.Code, result.Text );
    
    /* outputs
     6039 : Expected GE,S1S,S1E,S3S,S3E,S3A,SVA,ST data segments under Functional Group context.
    14056 : Transaction set/Message header missing.
    12417 : Number of included transaction sets/messages 1 does not match actual count of 0             
     * /


    Note check out the regex forum to begin your journey into regex's Regular Expressions and check out the top level announcements to find resources such as programs to use to create and manipulate as well as tutorials.

    HTH
    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaManModerator Thursday, April 23, 2009 3:57 PM Better code formatting
    • Marked as answer by Guo Surfer Sunday, April 26, 2009 5:57 AM
    Thursday, April 23, 2009 3:55 PM
    Moderator