Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,830
    Thanks
    21
    Thanked 157 Times in 148 Posts

    regex for CSV file

    I need a "smart" regex that will recognize all possible "formatting variations" of a CSV file line, and split the file line into the appropriate pieces...For example, some common variations of a CSV file's formatting (for a single line) include:

    Code:
    "data1";"data2";"data3";"data4"
    Code:
    "data1","data2","data3","data4"
    Code:
    data1,data2,data3,data4
    Code:
     data1;data2;data3;data4
    In my CSV files, double quotes will be escaped if they appear inside data fields enclosed by double quotes. For example,
    Code:
    "This is \"my\" data";"This is data; This is also data";"More Data"
    The regex that I need needs to differentiate between escaped double quotes and regular double quotes...and it needs to accommodate for the CSV file formatting variations shown above....

    I've done some of my own fiddling with this, and so far have just come away frustrated. Any ideas are welcome (including partial-regex or non-regex solutions).

    Thanks.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Code:
    (?:"(?:\\"|[^"])*"|[^;]*)
    - version for ";" as delimiter. Try fgetcsv() first.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •