Simplify a string with MATLAB script

3 views (last 30 days)
Lucas S
Lucas S on 27 Feb 2020
Commented: Lucas S on 28 Feb 2020
Hello !
I have a string format which looks like this (this is not always 'A' before '_' and numbers) :
Eq = 'A_1+((A_2+A_3)&(A_4+A_5))+A_6';
How can i simplify the string like this (with a script) :
Eq = 'A_(1+((2+3)&(4+5))+6)'
For me the simplest way would be to delete all 'A_' iteration except the first one and to add a '(' after the first 'A_' but i don't know how to do it in a script.
Thank you in advance for your help !
Edit : After Stephen answer review, i realized i missed a detail : sometimes the string can be like this :
==> 'A_number_(1+((2+3)&(4+5))+6)'
With multiple string for example : 'String_String_String_1' . Is there anyway to delete string and '_' behind a number ?

Accepted Answer

Stephen23 on 27 Feb 2020
Edited: Stephen23 on 27 Feb 2020
>> Eq = 'A_1+((A_2+A_3)&(A_4+A_5))+A_6';
>> Fq = regexprep(Eq, '^([A-Z]+_)(.*)', '$1\(${strrep($2,$1,'''')}\)')
Fq =
Lucas S
Lucas S on 28 Feb 2020
Hmmm ok i'm dumb i tried with your 1st post thinking it was the new one ... My bad thank you !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!