Regexp MatchCollection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drkefir
    New Member
    • Mar 2010
    • 3

    Regexp MatchCollection

    I have a problem with my regexp.
    The input to match are "telegrams" of various length.
    "Fields" within a telegram are separated by ","
    If a "," char is part of a field the field must be inclosed by quotes: (field1,"field2 a,field2b",fiel d3,...)

    The problem with the regexp I'm using is that it does not parse "empty fields" correct.

    This telegram will return 6 matches:
    "04",N,1,2, 3,4
    This telegram will return 6 matches:
    "04",N,1,2, ,4
    But this telegram will only return 5 matches:
    "04",N,1,,, 4

    Code:
    private static MatchCollection doMatch(string telegramString)
    {
       string pattern = "(\".+?\"|[^,]+|,,)";
       MatchCollection col = Regex.Matches(telegramString, pattern);
       return col;
    }
    I have very little experience of regexp, please help.

    //Fredrik.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Does it have to be a RegEx?
    Since all your items are separated by commas you could use String.Split which will give you a string[] in return. Some of your elements will be empty, but you will get back all your fields in correct order including the empties.

    Comment

    • drkefir
      New Member
      • Mar 2010
      • 3

      #3
      tlhintoq! I have considerd that approach. I cant just use the telegramString. Split(new char[] { ',' }); out of the box since the fields might include the comma char (should be fairly easy to solve with som ugly search and replace etc. before doing the Split)
      But when looking at the design I have base class that implements methods like this one:
      protected abstract bool checkFieldCount (MatchCollectio n col);
      And Im not that found of breaking the design at this point. (multiple classes/implementations derives from this).

      Convert.ToMatch Collection(stri ng[]) ??????

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        If your data contains commas... and your field delimiters are commas... I don't see how any system would know where to break...

        Unless your data with commas is within quotes. Is it?

        Comment

        • drkefir
          New Member
          • Mar 2010
          • 3

          #5
          Rtfm ;) ............... ............... ........

          Comment

          Working...