필터 지우기
필터 지우기

String parsing with a delimiter

조회 수: 3 (최근 30일)
Lucas
Lucas 2012년 5월 16일
Hello, I’m having a little problem parsing a string using a delimiter. It leave the delimiter attached to my string. Here’s my code:
str = counter=10;count=7;;
[token, remain] = strtok(str, '=');
switch token
case 'counter'
[token1, remain1] = strtok(remain, ';');
counter = str2double(token1);
end
token comes out right but remain has the value of “=10;count=7;” so when I do my next strtok my token1 has a value of “=10”’. Is there any way to parse to a delimiter and remove it from my string in matlab? Thanks.

채택된 답변

Matt Kindig
Matt Kindig 2012년 5월 16일
To expand upon that, you can do something like this to loop through the entire string.
str = 'counter=10;count=7';
parts = regexp( str, '(?<arg>\w+)=(?<val>\d+);', 'names');
for k=1:length(parts),
arg = parts(k).arg;
switch arg,
case 'counter',
counter = str2double(parts(k).val);
case 'count',
%do something else.
%add additional cases as necessary.
end
end
  댓글 수: 1
Matt Kindig
Matt Kindig 2012년 5월 16일
Glad I could be of assistance. Regular expressions are super-powerful once you learn them. I highly recommend looking them up in the Matlab help.

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

추가 답변 (1개)

Matt Kindig
Matt Kindig 2012년 5월 16일
What is your desired output? You might be able to do this easier with regular expressions.
doc regexp

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by