How to replace first substring if regexprep doesn't work

1 回表示 (過去 30 日間)
beda meda
beda meda 2013 年 2 月 12 日
Hi!
I have some task in which I need sometimes to replace the first substring with another. In this task, I can get inside the string specific symbols, like for example plus (+). Here is my usecase:
stack='X+S+X+S';
left_side='X+S';
right_side='S';
Now I need to replace the first 'X+S' with 'S'. strrep doesn't work for me, because I get
stack=strrep(stack, left_side, right_side);
stack='S+S';
So I used regexprep, but that doesn't work either, in fact it does nothing
stack=regexprep(stack, left_side, right_side, 'once');
stack='X+S+X+S';
I guess it's because plus symbol has some meaning in regexp. So is there any solution for me? Solution that will replace only the first substring with the possibility of any symbols in it?
Thanks for anything!

採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 12 日

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 12 日
stack='X+S+X+S'
left_side='X+S'
right_side='S'
regexprep(stack, 'X+++S', 'S',1)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by