regex replace whole word

조회 수: 9 (최근 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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by