Adder
Adder

Reputation: 5868

CSV file pattern matching matches separators also

I have used this regex (?:^|;)\s*(?:(?:(?=")"([^"].*?)")|(?:(?!")(.*?)))(?=;|$) which is slightly adapted from a solution in this question

My problem with it is it also matches the separating semicolon, which i then have to remove manually, which is bad style.

String separator = ";";
String patternString = "(?:^|" + separator + ")\\s*(?:(?:(?=\")\"([^\"].*?)\")|(?:(?!\")(.*?)))(?=" + separator + "|$)";
pattern = Pattern.compile(patternString);

Matcher match = pattern.matcher(line);
                        // Find all cells and add them to row.
                        while (match.find())
                        {
                                String cell = match.group();
                                //HACK something with the pattern to match is wrong
                                if(cell.indexOf(";") == 0) cell = cell.substring(1);
                                cell = unescapeCsv(cell);
                                //do something with cell
}

I try to match this, and it has to match a column (see last column) that is quoted and has semicolon as part of the data:

Name;inst_type;Position;Currency;cftype;amount;tenor;fwterm;compoundtype;resetterm;histrates;sdate;edate;fwdate;callput;capfloor;strike;vola;volavalue;ulspot;ulspotvalue;divcurve;cleanflag;fixrate;compfr;dayc;refindex;spread;floatfactor;paystart;payend;annuity;amortizingtype;cgm;resrate;nextrate;isprorated;rolldate;rollday;islongstub;isarrear;payrule;paydays;paycal;resrule;resdays;rescal;isfixcoupon;spreadcurve;cds_spread_value;recovrate;payoutrate;payrec;creditspread;disc;"C""F"
Bond1;;1;EUR;2;100;6M;;;;;01.09.2007;01.09.2010;;;;;;;;;;;0,0625;1;1;;;;0;100;;;1;;;1;01.06.2008;;1;;;;;;;;;;;;;;;IR-EUR;"2;01062008;100;0;0.0625;;01092007;01062008";"2;01122008;100;0;0.0625;;01062008;01122008";"2;01062009;100;0;0.0625;;01122008;01062009";"2;01122009;100;0;0.0625;;01062009;01122009";"2;01092010;100;0;0.0625;;01122009;01092010";"1;01092010;100";
Bond2;;1;EUR;2;100;6M;;;;;01.09.2007;01.09.2010;;;;;;;;;;;0,0625;1;1;;;;0;100;;;-1;;;1;;;;;;;;;;;;;;;;;;IR-EUR;"2;01092010;100;0;0.0625;;01032010;01092010";"2;01032010;100;0;0.0625;;01092009;01032010";"2;01092009;100;0;0.0625;;01032009;01092009";"2;01032009;100;0;0.0625;;01092008;01032009";"2;01092008;100;0;0.0625;;01032008;01092008";"2;01032008;100;0;0.0625;;01092007;01032008";"1;01092010;100";

Upvotes: 0

Views: 822

Answers (2)

Stephane Janicaud
Stephane Janicaud

Reputation: 3627

Try this one, it should work : (?<=^|;)".*?"(?=;|$)|(?<=^|;)[^;]*(?=;|$)

Explanation

(?<=^|;)".*?"(?=;|$) Any character between double-quotes zero to unlimited times precedeed by start anchor or a semicolon and followed by a semicolon or end anchor

| OR

(?<=^|;)[^;]*(?=;|$) Any character but a semicolon zero to unlimited times precedeed by start anchor or a semi-colon and followed by a semicolon or end anchor

Use * instead of + is important to match empty columns

Demo

Upvotes: 2

grundic
grundic

Reputation: 4921

Just use normal CSV parser:

Upvotes: 0

Related Questions