Replacing characters in a string
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi,
I hope you can help on this little task I have. Basically I have a cell array and I want to know if this cell array contains any elements with 'ly' at the end of the word. If it does remove it from the word.
For example
strcell = {'hi' 'to' 'all' 'the' 'friendly' 'people' 'quickly' 'making' 'time' 'for' 'others'}
I understand that I can use "regexp" to find words that have these characters I want. From this I deduced *note that ive used match to just make it easy for me to see the word and not a random index value':
regexp(strcell, '\w+ly', 'match')
this should return friendly and quickly and from these words the "ly" should be removed:
* friendly -> friend
* quickly -> quick
I understand I am close with using regexprep However, from there I get confused on how to do this!
Thanks
댓글 수: 1
Sean de Wolski
2013년 1월 10일
Every new poster should read this question before posting. Very well done!
채택된 답변
Sean de Wolski
2013년 1월 10일
One way:
regexprep(strcell,'ly\>','')
Match ly at the end of the word \>. Replace it with nothing.
댓글 수: 1
Medhini B
2020년 8월 21일
What if I want to remove ly which is in the beginning of the word? Example "lyfriend , ...."
추가 답변 (2개)
Daniel Shub
2013년 1월 10일
편집: Daniel Shub
2013년 1월 10일
I think regexprep does what you want. You need to modify the regexp a little bit:
s = regexprep(strcell, '(\w+)ly', '$1')
If you only want at the end
s = regexprep(strcell, '(.*)(ly\>)', '$1')
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!