Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex question, parsing csv files

Status
Not open for further replies.

edpatterson

IS-IT--Management
Feb 24, 2005
186
I am parsing a csv file with nine fields. I tried
[tt]
chomp;
/,(.*)^/;
[/tt]
thinking it would grab just the last field, nope. I would like to avoid having 9 sets of (.*), if possible.

Ed
 
if you would like the last value then try:-

/.*,([^,]+)$/;


Kind Regards
Duncan
 
you could and probably should use a module like Text::CSV to read the CSV file, or you could do this:

Code:
chomp;
print (split(/,/))[8];

you are using the beginning of string anchor "^" in your regexp when you meant to use the end of string anchor "$"








 
KevinADC is right - CSVs seem simple but there are pitfalls for the unwary, like embedded commas for a start. Use one of the CSV parser modules to avoid re-inventing the wheel.
 
<Twack!> Sound of smacking oneself in the head.

Changing ^ to $ fixed the problem.

I'll also be reading up on Text::CSV since reinventing the wheel is not one of my favorite things to do.

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top