필터 지우기
필터 지우기

How to use regexprep to modify strings while keeping constituent numbers intact?

조회 수: 12 (최근 30일)
I have a cell array of strings:
str = {'$d(V_1)$', ...
'$d(V_2)$', ...
'$d(V_3)$'}
str = 1×3 cell array
{'$d(V_1)$'} {'$d(V_2)$'} {'$d(V_3)$'}
I wish to convert the cell array of strings into:
strDesired = {'$function(a_{d(V_1)})$', ...
'$function(a_{d(V_2)})$', ...
'$function(a_{d(V_3)})$'}
strDesired = 1×3 cell array
{'$function(a_{d(V_1)})$'} {'$function(a_{d(V_2)})$'} {'$function(a_{d(V_3)})$'}
I tried using regexprep but I do not know how to extract and put in the same number in the new string strNew as the original str. Here I'm replacing the number with x just to demonstrate my incomplete solution:
patternToFind = 'd\(V_[1-9]\)';
patternToReplaceWith = 'function\(a_\{d\(V_x\)\}\)';
strNew = regexprep(str, patternToFind, patternToReplaceWith)
strNew = 1×3 cell array
{'$function(a_{d(V_x)})$'} {'$function(a_{d(V_x)})$'} {'$function(a_{d(V_x)})$'}
Could someone assist me in forming a patternToReplaceWith which will help me arrive at strDesired after performing the regexprep?

채택된 답변

Voss
Voss 2022년 6월 10일
편집: Voss 2022년 6월 10일
You can capture the V_1, V_2, etc., in tokens, then place those tokens in the output of regexprep by specifying $1 in patternToReplaceWith
str = {'$d(V_1)$', ...
'$d(V_2)$', ...
'$d(V_3)$'};
patternToFind = 'd\((V_[1-9])\)';
% ^ ^ I added parentheses here to capture tokens of the form V_[1-9]
patternToReplaceWith = 'function\(a_\{d\($1\)\}\)';
% ^^ tell regexprep to use token #1 (the only token) #1
strNew = regexprep(str, patternToFind, patternToReplaceWith)
strNew = 1×3 cell array
{'$function(a_{d(V_1)})$'} {'$function(a_{d(V_2)})$'} {'$function(a_{d(V_3)})$'}
  댓글 수: 2
Aryan Ritwajeet Jha
Aryan Ritwajeet Jha 2022년 6월 10일
Thank you @Voss. Those parantheses are helpful for understanding exactly what token is extracted. I too was writing my (inefficient) solution not knowing that another user had already answered it in the meantime :D
Voss
Voss 2022년 6월 10일
You're welcome! I'm glad you got it figured out. Thanks for accepting my answer anyway!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Aryan Ritwajeet Jha
Aryan Ritwajeet Jha 2022년 6월 10일
Note: I'm answering my own question after seeing an apparent solution to my problem on MATLAB's help topic on Replace text using regular expression - MATLAB regexrep under the subheading Include Tokens in Replacement Text. I'll however wait for somene else to answer before possibly accepting this answer as I'm pretty sure there are more efficient ways to do the task.
str = {'$d(V_1)$', ...
'$d(V_2)$', ...
'$d(V_3)$'}
str = 1×3 cell array
{'$d(V_1)$'} {'$d(V_2)$'} {'$d(V_3)$'}
patternToFind = 'd\(V_(\w)\)';
Here \w looks for one word after V_ , which in the case of str is a number.
patternToReplaceWith = 'function\(a_\{d\(V_$1\)\}\)';
Here the $1 token extracts the first 'word' which was located by \w and inserts it into the replaced strings.
strNew = regexprep(str, patternToFind, patternToReplaceWith)
strNew = 1×3 cell array
{'$function(a_{d(V_1)})$'} {'$function(a_{d(V_2)})$'} {'$function(a_{d(V_3)})$'}

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by