Reuse parts of the matching expression as replace string
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I stuck in the usage of the regexprep() function and hope sb. can point me to the correct solution.
myarray = {'ABC 500mm';'DEF 3mm';'GHI 0.1mm'};
output = cellfun(@(myarray) regexprep(myarray,'\d*+mm','${regexp($0,''\d*'',''match'')}'),myarray, 'UniformOutput',false);
This 'solution' sadly produces an error as it states that the replacement string does not output a real string to use here. What I try to do is pretty simple - erase all occurrences of 'mm' when found after a number.
Many thanks!
0 个评论
采纳的回答
Sean de Wolski
2013-11-7
编辑:Sean de Wolski
2013-11-7
myarray = {'ABC 500mm';'DEF 3mm';'GHI 0.1mm';'3mmm'};
regexprep(myarray,'(\d)mm','$1')
Explained:
capture a token () for any digit \d followed by mm. Replace whole expression with the token (i.e. the digit).
Also the regexp* family of functions can work on cellstr arrays :)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!