필터 지우기
필터 지우기

Reuse parts of the matching expression as replace string

조회 수: 4 (최근 30일)
Léon
Léon 2013년 11월 7일
편집: Adam Danz 2020년 8월 29일
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!

채택된 답변

Sean de Wolski
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 :)
  댓글 수: 2
Léon
Léon 2013년 11월 7일
Hey thx! :)
Adam Danz
Adam Danz 2020년 8월 29일
편집: Adam Danz 2020년 8월 29일
Nice explanation, Sean de Wolski!
Regular expressions are one of those things that you could use for years and learn something new every time you put one together.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by