Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Basics
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2009, 07:37 AM
Registered User
 
Join Date: Sep 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regex pattern to match carriage returns inside quotation marks?

Hello all,
Anyone here who can help me or point me in the right direction?

I have a CSV file like below...

ID,Name,Comment,Date
5,"Steve","Likes football and pizza",20090909
6,"Lisa","Likes to cook",20090905
7,"Nate","Reads a lot of books
Likes to run marathons",20090908

...and the carriage return in Nates comment creates problems for me, so I need to replace it with something else. A simple Replace will not work since it will turn the entire file into one line. I believe I need to use a pattern here - but I don't know how it should look.

This is what I'm trying to do, and "the_pattern" is where I fail (strText contains the entire CSV text):

Dim RE
Set RE = new RegExp
RE.IgnoreCase = True
RE.Global = True
re.Pattern = "the_pattern"
strNewText = re.replace(strText, "NEWLINE")

I guess the regex pattern should match something like "carriage returns inside quotation marks", so I can use it to replace those carriage returns with something else (to later on convert them back to carriage returns).

Any help or pointers on this is highly appreciated. I've tried on my own for a while, but cannot seem to do it right.

/Miikee
 
Old September 19th, 2009, 01:12 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

Hi,

Please check the sample code which will work even the data is in multiple lines in CSV file

Code:
CONN_STRING = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=F:\FOLDER_PATH\;Extensions=csv"
Set myConn = Server.CreateObject("ADODB.Connection")
Set myRs = Server.CreateObject("ADODB.Recordset")
myConn.open CONN_STRING
myRs.open "select * from [FILENAME.csv]",myConn
while not myRs.eof
 response.write myRs(0) & " :::: " & myRs(1) & "<br>"
 myRs.movenext
wend
myRs.close
myConn.close
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
 
Old September 19th, 2009, 03:02 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Excellent answer.

Miikee: Make careful note of the PATH used in the connection string. It must be the path to the *DIRECTORY* containing the CSV file, *NOT* the path to the file itself.

Then you specify the actual file in the SQL query. Because of the period on the file name, you must wrap the file name with [ ... ]

Om_Prakash coded that exactly correctly. I'm just writing this to make sure you understand the important points.

You might also want to look here:
http://www.carlprothman.net/Default....orMicrosoftJet
scroll down to " You can also open a Text file ..."
Read that and also the link to the MS knowledge base

Last edited by Old Pedant; September 19th, 2009 at 03:07 PM..
 
Old September 21st, 2009, 04:16 AM
Registered User
 
Join Date: Sep 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A big thank you to you Om_Prakash - that is indeed a much more elegant way to parse a CSV file than how I use to do it - and that it takes care of my multiple line problem as well is wonderful!

Also - thank you very much to Old Pedant for providing your links and making sure I understood the example right!

You've made my day - not only for helping me with this problem but also because it is great to see people taking their time to simply help someone else.

Thanks!

/Miikee
 
Old September 21st, 2009, 06:04 AM
Registered User
 
Join Date: Sep 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just in case anyone else get the same issue:

In my question example above I have headers in the CSV file - but now it turns out my CSV file actually wont.

This means that the data in the first line will not be returned as it will be treated as a line of headers. The only way I manage to include the first line is to change the connection string in om_prakash excellent example to the following:

CONN_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\FOLDER_PATH\;Extended Properties='text;HDR=NO;FMT=Delimited'"

By doing so, the data in the first line is returned as well.

/Miikee





Similar Threads
Thread Thread Starter Forum Replies Last Post
display carriage returns in label bleitner ASP.NET 2.0 Professional 4 May 21st, 2011 12:10 PM
for-each carriage returns TiLaser XSLT 4 April 28th, 2008 07:31 PM
How are Carriage Returns stored? SQLScott SQL Server 2005 2 December 1st, 2006 04:07 PM
How are Carriage Returns stored? SQLScott SQL Server 2000 3 November 24th, 2006 10:37 AM
apostrophes or quotation marks crmpicco Javascript 6 March 21st, 2006 09:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.