Replacing characters in a string

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
Sean de Wolski 2013 年 1 月 10 日
Every new poster should read this question before posting. Very well done!

サインインしてコメントする。

 採用された回答

Sean de Wolski
Sean de Wolski 2013 年 1 月 10 日

2 投票

One way:
regexprep(strcell,'ly\>','')
Match ly at the end of the word \>. Replace it with nothing.

1 件のコメント

Medhini B
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
Daniel Shub 2013 年 1 月 10 日
編集済み: Daniel Shub 2013 年 1 月 10 日

0 投票

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')

2 件のコメント

Tanil
Tanil 2013 年 1 月 10 日
I saw my problem I did try that once, but for some reason it kept outputting "$1" I had to parenthesis the \w+ part. Cheers for that
Tanil
Tanil 2013 年 1 月 10 日
O sorry this does it when 'ly' is also located in the middle of the word. I want it to do it providing its at the end only!

サインインしてコメントする。

Tanil
Tanil 2013 年 1 月 10 日

0 投票

after not skimming the regular expression chapter. There is another method ... I guess there is many different combinations.
[^c1c2c3] }
Any character not contained within the brackets: anything but c1 or c2 or c3
deducing from this, the '^' is like ~ and works as a not therefore, [^a-z]
Thanks for all the suggestions.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2013 年 1 月 10 日

コメント済み:

2020 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by