Replacing commas with whitespaces using regexprep

Hello,
i am new to Matlab and really struggling with the regexprep function. I try to replace commas in brackets with whitespaces, so i can use the split function, without splitting my data in brackets.
Task:
str= '(asdf,(50,51,52),jklö)'
str_desired='(asdf,(50 51 52),jklö)'
I already found this:
exp='(?<=\()[^)]*(?=\))'
rep=' '
newstr=regexprep(str,exp,rep)= '( ),jklö)'+
But its not quite doing what i want, and i cant figure out how to place the hexadecimalvalue '\x2C' for comma.
Thank you very much!

 채택된 답변

Stephen23
Stephen23 2019년 6월 18일
>> str = '(asdf,(50,51,52),jkl)';
>> regexprep(str,'(\d+),(\d+),(\d+)','$1 $2 $3')
ans = (asdf,(50 51 52),jkl)
If you are already using regexprep I don't see the point in using strsplit as well, you might as well just use regexp to split the string up.

댓글 수: 6

Hello Stephen,
thank you very much for your fast answer. I am sorry i have to specify my problem a little bit better. My example was just to bad :(
My data looks more like this:
('1ywu2rkZn3CfItlgI791$i',#41,'Pset_WindowCommon',$,(#279,#2098,#2099))
('1jPCssxb5C1RSM_YLIx$zl',#41,$,$,(#854,#889,#924,#1062,#1096,#1130,#1275,#2812,#2833),#135)
So the amount of arguments is totally variable. I have to split for every "field" which are divided by commas. So data in brackets belongs to only one field. Thats why i tried to work this code example:
exp=(?<=\() [^)]* (?=\)) [Look behind "(" , every character which is not ")" , look before ")"]
But i couldnt figure out how i can replace the middle ("*") with the indicator for commas. So only commas in brackets get replaced.
Thank you in advance, cheers!
Stephen23
Stephen23 2019년 6월 18일
편집: Stephen23 2019년 6월 18일
@Raymond Wollenberg: please upload some sample data in a .mat file, by clicking the paperclip button. Or the original data source file.
Stephen23
Stephen23 2019년 6월 18일
편집: Stephen23 2019년 6월 18일
This should get you started. Note that for simplicity I excluded the leading/trailing parentheses from each string (you can do this trivially using indexing).
>> R = '(\(?)(?(1)[^\)]+\)|[^,]+)'; % regular expression.
>> S = '''1ywu2rkZn3CfItlgI791$i'',#41,''Pset_WindowCommon'',$,(#279,#2098,#2099)';
>> D = regexp(S,R,'match');
>> D{:}
ans =
'1ywu2rkZn3CfItlgI791$i'
ans =
#41
ans =
'Pset_WindowCommon'
ans =
$
ans =
(#279,#2098,#2099)
>> S = '''1jPCssxb5C1RSM_YLIx$zl'',#41,$,$,(#854,#889,#924,#1062,#1096,#1130,#1275,#2812,#2833),#135';
>> D = regexp(S,R,'match');
>> D{:}
ans =
'1jPCssxb5C1RSM_YLIx$zl'
ans =
#41
ans =
$
ans =
$
ans =
(#854,#889,#924,#1062,#1096,#1130,#1275,#2812,#2833)
ans =
#135
It uses a conditional operator to change the match expresssion, depending on whether an opening parenthesis was matched at the start of the field.
You might also be interested to download my interactive regular expression tool, which lets you quickly test and develop regular expressions:
Thank you very much again!
I already installed your tool on midday :D in hope I can use my problem with it. Thanks for explanation, i will dig into it tomorrow!
Worked out perfect, thank you!

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by