How do I ignore delimiter inside quotation marks using regexp?

Hi everyone,
I'm writing a script to import data. The size (coloumns) of the data can vary. The main delimiter is a semicolon.
My data looks like the following:
Header ; Value ; Data
Example1; 12 ; "air"
Example2; 24 ; "water"
For this usecase I'm using
filebyfield = regexp(filebyline, ';', 'split'); %filebyline- textfile including my data
Unfortunately the data I try to import isn't uniform and could look like the following:
Header ; Value ; Data
Example1; 12 ; "air;fire"
Example2; 24 ; "water"
Is there any way to ignore the delimiter (";") if its inside of quotation marks? I thought about textscan, but I don't have the same number of coloumns every time and therefore cant specify the data format.
Any help would be appreciated! Best regards

 채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 4일

1 개 추천

sometimes the easiest way is to detect the quoted delimiter and replace them with some special character such as ¡ and then do the split and then fix-up the results.

댓글 수: 4

hi walter, ok thanks for the hint, I tried it but it turned out that my whole approach was too complicated for the task.
best regards!
I think Walter's approch is quite simple. For example
filebyfield = {'Header ; Value ; Data';
'Example1; 12 ; "Air;fire"';
'Example2; 24 ; "water"'};
filebyfield = regexprep(filebyfield, '("\w*);(\w*")', '$1@$2');
filebyfield = regexp(filebyfield, ';', 'split');
filebyfield = cellfun(@(x) {strrep(x, '@', ';')}, filebyfield)
Thanks alot! It works perfectly.
I have an additional question about the first line:
filebyfield = regexprep(filebyfield, '("\w*);(\w*")', '$1@$2');
Could you please tell me if my assumptions about the terms
'("\w*);(\w*")' % finds the quoted ";" | "/w*" means characters with undefined length
and
'$1@$2' % "$1" stands for the first "("\w*)" and $2 for the second "("\w*)"
are correct?
Yes, all the points are correct.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2020년 11월 4일

댓글:

2020년 11월 6일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by