473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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,"field2a,field2b",field3,...)

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

Expand|Select|Wrap|Line Numbers
  1. private static MatchCollection doMatch(string telegramString)
  2. {
  3.    string pattern = "(\".+?\"|[^,]+|,,)";
  4.    MatchCollection col = Regex.Matches(telegramString, pattern);
  5.    return col;
  6. }
I have very little experience of regexp, please help.

//Fredrik.
Mar 3 '10 #1
4 3828
tlhintoq
3,525 Expert 2GB
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.
Mar 3 '10 #2
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(MatchCollection col);
And Im not that found of breaking the design at this point. (multiple classes/implementations derives from this).

Convert.ToMatchCollection(string[]) ??????
Mar 3 '10 #3
tlhintoq
3,525 Expert 2GB
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?
Mar 3 '10 #4
Rtfm ;) ......................................
Mar 4 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
1
by: Mortimer Schnurd | last post by:
Has anyone had any luck getting this CopyTo method to work? I can iterate through a MatchCollection and move each Match.Value to the System.Array without a problem. I just can't figure out why...
0
by: Victoria Kagansky | last post by:
Hi! Has anybody seen the following problem? The Regex.Matches function returns a MatchCollection object containing all successful matches of the expression in provided string. In several cases...
6
by: Kini | last post by:
In the program below, when the control comes back to the Main program, a considerable amount of memory is still associated with the program. Is this normal? Is it due to repeated generation of...
4
by: Chris Beach | last post by:
Hi there, I'm attempting to extract a number from a string, and I'm using the following code: Regex prRegexp = new Regex( @"Rank_1:\d+:(\d+)" ); MatchCollection matches = prRegexp.Matches(...
2
by: Eric Gibson | last post by:
Hey all, I'm trying to do a regexp that will gracefully extract the fields (fn) from a string in this format: (f1, f2, f3),(f1, f2, f3),(f1, f2, f3),...,(f1, f2, f3) Is there a way I could...
27
by: SQL Learner | last post by:
Hi all, I have an Access db with two large tables - 3,100,000 (tblA) and 7,000 (tblB) records. I created a select query using Inner Join by partial matching two fields (X from tblA and Y from...
5
by: Davros9 | last post by:
Trying to get Regular Expressions working....... ---------------- Public Function SepString(InField As String) As String ''seperates on space and comma Dim RE As New RegExp Dim Matches As...
3
by: Jeannot | last post by:
Dim re As New Regex("\s(+)\s") Dim mc As Match = re.Match("Puppy measures 13 inches ") Dim s As String = mc.Groups.Item(0).ToString() s is " 13 " with a leading and a trailing...
10
by: Andy B | last post by:
Is it safe to make a method that returns a match collection or nothing? or is it better to just return a match collection and have the code outside the method validate that match collection is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.