473,661 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expressions to parse a CSV line

MW
Dear All

Does anyone have a regular expression to parse a comma delimited line with
some fields optionally having string delimiters (text qualifiers)
I am currently testing with this regular expression and it works in almost
all my test cases. I found this on the internet in a C# solution.

,(?=([^\"]*"[^"]*")*(?![^"]*"))

However in some of my test cases it fails and I am having difficulty
interpreting it.

The VB.NET function I used is

Public Function parseCSVLine(By Val sInputString As String) As ArrayList
Dim r As New Regex(",(?=([^\" & Chr(34) & "]*" & Chr(34) & "[^" &
Chr(34) & "]*" & Chr(34) & ")*(?![^" & Chr(34) & "]*" & Chr(34) & "))")
Dim iStart As Integer, m As Match
Dim oArrayList As New ArrayList()

For Each m In r.Matches(sInpu tString)
oArrayList.Add( sInputString.Su bstring(iStart, m.Index - iStart))
iStart = m.Index + 1
Next
oArrayList.Add( sInputString.Su bstring(iStart, sInputString.Le ngth -
iStart))

Return oArrayList
End Function

My test cases are as follows:
#
CSV
Value 1
Value 2
Value 3
Value 4
Results

1
a,b,c
a
b
c

P

2
"a",b,c
a
b
c

P

3
'a',b,c
'a'
b
c

P

4
a , b , c
a
b
c

P

5
aa,bb;cc
aa
bb;cc
P

6

P

7
a
a

P

8
,b,

b
P

9
,,c
c

P

10
,,


P

11
"",b

b
P

12
" ",b
[SPACE]
b
P

13
"a,b"
a,b

P

14
"a,b",c
a,b
c
P

15
" a , b ", c
a , b
c
P

16
a b,c
a b
c
P

17
a"b,c
a"b
C
P

18
"a""b",c
a"b
c
P

19
a""b,c
a""b
c
P

20
a,b",c
a
b"
c

O

21
a,b"",c
a
b""
c

P

22
a,"B: ""Hi, I'm B""",c
a
B: "Hi, I'm B"
c

P

23
a,"b,c
a
"b
c

O

24
a,bc"d,e
a
bc"d
e
O

25
a,bc"d",e
a
bc"d"
e

O

26
a,"bc"d,e
a
"bc"d
e

O

Many thanks,
Wazir
Jul 21 '05 #1
18 12333
MW
Apologies for the formatting of test cases, I didnt realise I was posting in
Plain Text.

Here they are again, I hope it is more readable this time.

Some of them like case 20 doesnt work with the regular expression

# CSV Value 1 Value 2 Value 3 Value 4
1 a,b,c a b c
2 "a",b,c a b c
3 'a',b,c 'a' b c
4 a , b , c a b c
5 aa,bb;cc aa bb;cc
6
7 a a
8 ,b, b
9 ,,c c
10 ,,
11 "",b b
12 " ",b [SPACE] b
13 "a,b" a,b
14 "a,b",c a,b c
15 " a , b ", c a , b c
16 a b,c a b c
17 a"b,c a"b c
18 "a""b",c a"b c
19 a""b,c a""b c
20 a,b",c a b" c
21 a,b"",c a b"" c
22 a,"B: ""Hi, I'm B""",c a B: "Hi, I'm B"
c
23 a,"b,c a "b c
24 a,bc"d,e a bc"d e
25 a,bc"d",e a bc"d" e
26 a,"bc"d,e a "bc"d e
Jul 21 '05 #2
MW
Apologies for the formatting of test cases, I didnt realise I was posting in
Plain Text.

Here they are again, I hope it is more readable this time.

Some of them like case 20 doesnt work with the regular expression

# CSV Value 1 Value 2 Value 3 Value 4
1 a,b,c a b c
2 "a",b,c a b c
3 'a',b,c 'a' b c
4 a , b , c a b c
5 aa,bb;cc aa bb;cc
6
7 a a
8 ,b, b
9 ,,c c
10 ,,
11 "",b b
12 " ",b [SPACE] b
13 "a,b" a,b
14 "a,b",c a,b c
15 " a , b ", c a , b c
16 a b,c a b c
17 a"b,c a"b c
18 "a""b",c a"b c
19 a""b,c a""b c
20 a,b",c a b" c
21 a,b"",c a b"" c
22 a,"B: ""Hi, I'm B""",c a B: "Hi, I'm B"
c
23 a,"b,c a "b c
24 a,bc"d,e a bc"d e
25 a,bc"d",e a bc"d" e
26 a,"bc"d,e a "bc"d e
Jul 21 '05 #3
You can use the OleDbCommand class to read a csv file

Tu-Thac
www.ongtech.co

----- MW wrote: ----

Dear Al

Does anyone have a regular expression to parse a comma delimited line wit
some fields optionally having string delimiters (text qualifiers
I am currently testing with this regular expression and it works in almos
all my test cases. I found this on the internet in a C# solution

,(?=([^\"]*"[^"]*")*(?![^"]*")

However in some of my test cases it fails and I am having difficult
interpreting it

The VB.NET function I used i

Public Function parseCSVLine(By Val sInputString As String) As ArrayLis
Dim r As New Regex(",(?=([^\" & Chr(34) & "]*" & Chr(34) & "[^"
Chr(34) & "]*" & Chr(34) & ")*(?![^" & Chr(34) & "]*" & Chr(34) & "))"
Dim iStart As Integer, m As Matc
Dim oArrayList As New ArrayList(

For Each m In r.Matches(sInpu tString
oArrayList.Add( sInputString.Su bstring(iStart, m.Index - iStart)
iStart = m.Index +
Nex
oArrayList.Add( sInputString.Su bstring(iStart, sInputString.Le ngth
iStart)

Return oArrayLis
End Functio

My test cases are as follows

CS
Value
Value
Value
Value
Result
a,b,



"a",b,



'a',b,
'a


a , b ,



aa,bb;c
a
bb;c







,b



,,


1
,


1
"",


1
" ",
[SPACE

1
"a,b
a,

1
"a,b",
a,

1
" a , b ",
a ,

1
a b,
a

1
a"b,
a"

1
"a""b",
a"

1
a""b,
a""

2
a,b",

b


2
a,b"",

b"


2
a,"B: ""Hi, I'm B""",

B: "Hi, I'm B


2
a,"b,

"


2
a,bc"d,

bc"

2
a,bc"d",

bc"d


2
a,"bc"d,

"bc"


Many thanks
Wazi

Jul 21 '05 #4
You can use the OleDbCommand class to read a csv file

Tu-Thac
www.ongtech.co

----- MW wrote: ----

Dear Al

Does anyone have a regular expression to parse a comma delimited line wit
some fields optionally having string delimiters (text qualifiers
I am currently testing with this regular expression and it works in almos
all my test cases. I found this on the internet in a C# solution

,(?=([^\"]*"[^"]*")*(?![^"]*")

However in some of my test cases it fails and I am having difficult
interpreting it

The VB.NET function I used i

Public Function parseCSVLine(By Val sInputString As String) As ArrayLis
Dim r As New Regex(",(?=([^\" & Chr(34) & "]*" & Chr(34) & "[^"
Chr(34) & "]*" & Chr(34) & ")*(?![^" & Chr(34) & "]*" & Chr(34) & "))"
Dim iStart As Integer, m As Matc
Dim oArrayList As New ArrayList(

For Each m In r.Matches(sInpu tString
oArrayList.Add( sInputString.Su bstring(iStart, m.Index - iStart)
iStart = m.Index +
Nex
oArrayList.Add( sInputString.Su bstring(iStart, sInputString.Le ngth
iStart)

Return oArrayLis
End Functio

My test cases are as follows

CS
Value
Value
Value
Value
Result
a,b,



"a",b,



'a',b,
'a


a , b ,



aa,bb;c
a
bb;c







,b



,,


1
,


1
"",


1
" ",
[SPACE

1
"a,b
a,

1
"a,b",
a,

1
" a , b ",
a ,

1
a b,
a

1
a"b,
a"

1
"a""b",
a"

1
a""b,
a""

2
a,b",

b


2
a,b"",

b"


2
a,"B: ""Hi, I'm B""",

B: "Hi, I'm B


2
a,"b,

"


2
a,bc"d,

bc"

2
a,bc"d",

bc"d


2
a,"bc"d,

"bc"


Many thanks
Wazi

Jul 21 '05 #5
In article <#h************ *@tk2msftngp13. phx.gbl>, MW wrote:
Dear All

Does anyone have a regular expression to parse a comma delimited line with
some fields optionally having string delimiters (text qualifiers)


I just replied to a similar post.

String object has Split call:

"a,b,c,de,fg,he gk".Split(',' )
Jul 21 '05 #6
In article <#h************ *@tk2msftngp13. phx.gbl>, MW wrote:
Dear All

Does anyone have a regular expression to parse a comma delimited line with
some fields optionally having string delimiters (text qualifiers)


I just replied to a similar post.

String object has Split call:

"a,b,c,de,fg,he gk".Split(',' )
Jul 21 '05 #7
>> Does anyone have a regular expression to parse a comma delimited line with
some fields optionally having string delimiters (text qualifiers)
I just replied to a similar post.

String object has Split call:

"a,b,c,de,fg,he gk".Split(',' )


This is a problem that is known for a long time to the Perl programmers.
It cannot be solved with regular expressions only.
The simple string above is works, but what about the next one, also valid
CSV:

Item 1,"string with comma, not to split",1234,"no w, does it work?",done

This should be split into
Item 1
string with comma, not to split
1234
now, does it work?
done

--
Mihai
-------------------------
Replace _year_ with _ to get the real email
Jul 21 '05 #8
>> Does anyone have a regular expression to parse a comma delimited line with
some fields optionally having string delimiters (text qualifiers)
I just replied to a similar post.

String object has Split call:

"a,b,c,de,fg,he gk".Split(',' )


This is a problem that is known for a long time to the Perl programmers.
It cannot be solved with regular expressions only.
The simple string above is works, but what about the next one, also valid
CSV:

Item 1,"string with comma, not to split",1234,"no w, does it work?",done

This should be split into
Item 1
string with comma, not to split
1234
now, does it work?
done

--
Mihai
-------------------------
Replace _year_ with _ to get the real email
Jul 21 '05 #9
MW
Sergei,

Split will only work for a comma delimited file and will not work with text
qualifiers (string delimiters)

"Sergei Gnezdov" <no****@blackho le.net> wrote in message
news:sl******** ***********@STA 01052.itron.com ...
In article <#h************ *@tk2msftngp13. phx.gbl>, MW wrote:
Dear All

Does anyone have a regular expression to parse a comma delimited line with some fields optionally having string delimiters (text qualifiers)


I just replied to a similar post.

String object has Split call:

"a,b,c,de,fg,he gk".Split(',' )

Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1835
by: dmbkiwi | last post by:
I have a problem. I have written a python based theme for a linux app called superkaramba, which is effectively an engine for desktop applets that utilises python as its theming language. The script basically parses weather websites, and displays the info in a visually appealing way. A couple of other people have contributed code to this project, particularly relating to the parsing of the websites. Unfortunately, it is not parsing...
8
2423
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average: 0.04, 0.02, 0.01" how can I extract this number with RE or otherwise?
11
3900
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category I am looking to retrive name value...
10
2874
by: hclugano | last post by:
Hi! There is a SQL Create Table statement, for example: CREATE TABLE . ( IDENTITY (1, 1) NOT NULL, NOT NULL, ( 15) NOT NULL ..... ) ON
8
5025
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and I need to parse strings that look similar to the one below. All 5 rows will make up one string. I have a form where a use can copy/paste data like what you see below from excel, word, notepad, etc.. into a textbox on my form. I need to break each line into 2 numbers which I'll use as parameters for another function. in all cases each line will be separated with a vbNewline and in most cases the 2...
18
479
by: MW | last post by:
Dear All Does anyone have a regular expression to parse a comma delimited line with some fields optionally having string delimiters (text qualifiers) I am currently testing with this regular expression and it works in almost all my test cases. I found this on the internet in a C# solution. ,(?=(*"*")*(?!*")) However in some of my test cases it fails and I am having difficulty
6
1406
by: John Salerno | last post by:
Ok, this might look familiar. I'd like to use regular expressions to change this line: self.source += '<p>' + paragraph + '</p>\n\n' to read: self.source += '<p>%s</p>\n\n' % paragraph Now, matching the middle part and replacing it with '%s' is easy, but
25
5147
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
13
7473
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can be accomplished with regular expressions this way, such as validating a mathematical expression or parsing a language with nested parens, quoting or expressions. Another feature I'm missing is once-only subpatterns and possessive quantifiers...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.