Dynamic regular expression replacement: token number
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I was trying to do something that sounded simple... but I'm stuck!
Trying to replace matched tokens using regexprep, but I'd like to replace them with there token number dynamically.
For example:
regexprep('(first matched_token)_fjezhfjksdhf_(second matched_token)','matched_token', 'matchednumber_$DUNNO?')
would output
(first matchednumber_1)_fjezhfjksdhf_(second matchednumber_2)
Any idea appreciated, thanks!
0 个评论
回答(2 个)
Sai Veeramachaneni
2020-3-23
You can use something similar to below code to simulate the required behavior.
str='(first matched_token)_fjezhfjksdhf_(second matched_token)';
i=0;
while true
i=i+1;
temp=regexprep(str,'matched_token',strcat("matchednumber_",num2str(i)),'once');
if isequal(temp,str)
break
end
str=temp;
end
str
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!