Need some help on regex.split function

Yafuhenk

Active Member
Licensed User
Longtime User
Hi,

It might be very simple but I can't find the solution.
I try to import a csv file via textreader and I like to split the line.
The values are between double quotes and separated with ';'
Example:
"Company1";"Product2";"Country3";"Turnover4"

Using Regex.Split(";", line) gives
"Company1"
"Product2"
"Country3"
"Turnover4"

I want however the following:
Company1
Product2
Country3
Turnover4

Using "";"" is not the solution.

Any ideas?

Thanks in advance

Henk
 

Yafuhenk

Active Member
Licensed User
Longtime User
Thanks for your reply,

The csv file however is rather large (about 7M).
StringUtils.LoadCSV let the program crash, where the Textreader works.
So I can't use this solution.

Henk
 
Last edited:
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
You could use a replace of quote to empty string and the split on ;.

The main problem arises when you have a ; within the quotes. It would be smarter to replace ";" by || (for example) and then replace all quotes left by an empty string (to get rid of the first and last quote per line). After that you do a split on || and you have a better result.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
If you want to use Regex you could do something like this:

B4X:
Dim Values As List
Dim Buffer As String
    
Values.Initialize
    
Line = QUOTE & "Company1" & QUOTE & ";" & QUOTE & "Product2" & QUOTE 'This is just a sample.

Values = Regex.Split(";", Line)

For I = 0 To Values.Size - 1

    Buffer = Values.Get(I)
    Buffer = Buffer.Replace(QUOTE, "")
        
    Msgbox(Buffer, "")

Next
 
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
@ NJDude
Buffer = Buffer.Replace(QUOTE, "") did the job.
Thank you so much!!
@ COBRASoft
I might need you suggestions as well since for some rows I get an index out of bound error. Some article descriptions contains ';'
I like to thank you as well!

Henk
 
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
@Erel,

That is exactly what I want to do. But I have to load the CSV first to import it in the SQLite database. This loading of a large csv file for import doesn't work with StringUtils.

Regards

Henk
 
Upvote 0
Top