replace multiple "1" with only one "1"

조회 수: 3 (최근 30일)
Andy
Andy 2014년 4월 20일
댓글: Jan 2014년 4월 21일
first, I have a strig array with all kinds of symbols, for example:
FF1 = ('eE_?@ wwqy W');
then, I want to first turn FF2 into ASCII: FF2 =abs(FF1) . replace all the symbols with 32 ( the corresponding number for empty space). then I wanto to delete every continuous 32s into one 32. After replacement,
FF3 = ' 101 69 95 32 32 32 32 32 119 119 113 121 32 32 32 87'
the result i got should be
FF3 = ' 101 69 95 32 119 119 113 121 32 87'
this question is similar to " replace multiple space with one space", which could be done by
txt_new = regexprep(text,' +',' '). Instead, I use FF2 = regexprep(FF2, ' 32+',' 32'). But I failed.so how should I do?I attached the codes
another problem, it is only to use regexprep for str, can any function directly deal with number array?
FF2 = abs('eE_?@ wwqy W');
FF3 = num2str(FF2);
lol = regexprep(FF3, '\w*32+', ' 32');
  댓글 수: 2
Jos (10584)
Jos (10584) 2014년 4월 20일
Your title is not matching your question ...
John D'Errico
John D'Errico 2014년 4월 21일
Andy - why flag an answer? Just add a comment if all you wanted to do was to thank a responder. Flags serve a different purpose.

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

답변 (4개)

Jos (10584)
Jos (10584) 2014년 4월 20일
Why not operate on the string array and then convert it to ascii? Like this:
FF1 = 'eE_?@ wwqy W'
FF3 = double(regexprep(FF1,'[^a-zA-Z0-9_]+',' '))
  댓글 수: 3
dpb
dpb 2014년 4월 20일
편집: dpb 2014년 4월 20일
If speed is an issue, regexp is a bottleneck, for sure...I don't believe there's any basis at all for the assumption that character substitution isn't just as fast as conversion first--after all, in Matlab they're simply an array of char() internally, anyway.
What is the problem that requires solving that the means by which one gets from FF1 to FF3 matters?
Jos (10584)
Jos (10584) 2014년 4월 21일
Apparently, you have a different problem than the one you posted here. What is it exactly you want to accomplish?

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


Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 20일
FF1 = 'eE_?@ wwqy W'
FF2=abs(FF1)
FF3=FF2
a=[0 FF2==32 0];
ii1=strfind(a,[0 1])
ii2=strfind(a,[1 0])-1
FF3(cell2mat(arrayfun(@(x,y) x+1:y,ii1,ii2,'un',0)))=[]

dpb
dpb 2014년 4월 20일
I'd go at it differently, for sure...no real need for regexp() altho it'll work
>> ff3=FF1;
>> ff3(~isletter(ff3))=' ';
>> ff3=double(unique(ff3,'stable'))
ff3 =
101 69 32 119 113 121 87
>>
Looks to me like your solution missed replacing the underscore unless it's to be special-cased?
As for the question asked about dealing with it as numeric I don't see why you'd want to until the end, but unique will do the same thing there as did above.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 20일
The expected result is
FF3 = ' 101 69 95 32 119 119 113 121 32 87'
dpb
dpb 2014년 4월 20일
Actually, though unique probably isn't the answer as it'll do the reduction globally rather than just for the blanks unless you restrict to the subset of FF3(FF3==' ')

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


Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 20일
Try this
FF1 = 'eE_?@ wwqy W'
FF3=strjoin(strsplit(FF1))
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 21일
Andy, why have you flagged this answer?
Jan
Jan 2014년 4월 21일
I've removed the flag. There is obviously no reason for a flag.

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

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by