MATLAB Answers

0

regexprep - may the replacement string contain a variable?

Alexander さんによって質問されました 2012 年 12 月 13 日
My problem:
Assume you have the following string: str='WBEi xx(i,BE)'; I want to replace BE by QQ, but only in the second substring xx(i,BE).
The problem is that QQ is stored in a variable, say q='QQ'.
If I would take the function strrep(str, 'BE', q) both occurances of BE are replaced by QQ: ans = 'WQQi xx(i,QQ)'
If I realize it with regexprep (explicitely using 'QQ'): regexprep(str, '(xx\(i,)BE(\))', '$1QQ$2' ) the result is the right one: ans = 'WBEi xx(i,QQ)' %only the second is replaced
But is it somehow possible to parse the variable q='QQ' to this or a similiar function, to do the job?
Thanks!

  0 件のコメント

サインイン to comment.

タグ

2 件の回答

Matt Fig
回答者: Matt Fig
2012 年 12 月 13 日
 採用された回答

Does this work?
regexprep(str, '(xx\(i,)BE(\))', ['$1',q,'$2'] )

  0 件のコメント

サインイン to comment.


回答者: Alexander 2012 年 12 月 13 日

That's it. Many thanks.
Alex

  0 件のコメント

サインイン to comment.



Translated by