Correct use of regexp

조회 수: 1 (최근 30일)
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 2월 13일
댓글: GEORGIOS BEKAS 2018년 2월 13일
If x is the following string, I want to add a zero to the end of each sequence of zeros.
x='0110'
y=regexp(x,'[0+]','[0+]$0')
The result should be: x ='001100'

답변 (2개)

ES
ES 2018년 2월 13일
y=regexprep(x,'[0+]','00')
  댓글 수: 1
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 2월 13일
it does not work if the string would be: x='00011000'

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


Steven Lord
Steven Lord 2018년 2월 13일
x = char(randi([0 1], 1, 20)+'0')
y = regexprep(x, '0+', '$00')
J Smith's answer doubles the length of each run of 0's in x. Mine adds one 0 to the end of each run of 0's. In the case where all the runs are of length 1 they are the same. They aren't the same when you have runs of length greater than 1.
  댓글 수: 1
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 2월 13일
it does not work

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

카테고리

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