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!

 采纳的回答

>> 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!
@Raymond Wollenberg: please upload some sample data in a .mat file, by clicking the paperclip button. Or the original data source file.
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!

请先登录,再进行评论。

更多回答(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