フィルターのクリア

regex replace whole word

12 ビュー (過去 30 日間)
Bardo
Bardo 2018 年 6 月 1 日
コメント済み: Stephen23 2018 年 6 月 1 日
Hi,
How to change
'v1 = v1a;'
to
'v(1) = v1a;'
using regex or regexrep?
regexprep('v1 = v1a','\bv1\b','v(1)')
apparently does not work.

採用された回答

Stephen23
Stephen23 2018 年 6 月 1 日
編集済み: Stephen23 2018 年 6 月 1 日
>> regexprep('v1 = v1a','^([a-z]+)(\d+)','$1($2)')
ans = v(1) = v1a
Hopefully you are not constructing arbitrary strings of code for evaluating!
  8 件のコメント
Bardo
Bardo 2018 年 6 月 1 日
I reckon the main requested feature - to replace only whole words matching the string - is well in the title but got lost in the discussion.
Stephen23
Stephen23 2018 年 6 月 1 日
@Bardo: you should accept your answer, if it resolves your original question.

サインインしてコメントする。

その他の回答 (1 件)

Bardo
Bardo 2018 年 6 月 1 日
Here we go:
>> regexprep('v1 = v1a','\<v1\>','v(1)')
ans =
v(1) = v1a
or as a strrep for whole words
function snew = subsname(s, old, new)
snew = regexprep(s,['\<', old,'\>'], new);

カテゴリ

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

タグ

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by