Replace string with another value

2 ビュー (過去 30 日間)
klb
klb 2020 年 7 月 4 日
編集済み: klb 2020 年 7 月 4 日
What would be a good way replace a full string with first 3 letters of last word. For example '13 Jon Walker' with 'WAL' .
I have about 25 such replacements an this is indeed doable line-by-line "by-hand" using replace function, but that beats the purpose of MATLAB. Plus i might have more in future. I looked up regexprep too but it doesnt quite do that. OR have I missed it ? Is there a way I could apply a 'replace full string with first 3 letters of last word ' rule?
Thank you for your time in advance.

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 4 日
S = {'13 Jon Walker', '23 skidoo', '0 infinite way blvd'};
upper(regexprep(S, '^.*\s+(...).*', '$1'))
  1 件のコメント
klb
klb 2020 年 7 月 4 日
編集済み: klb 2020 年 7 月 4 日
Thank you this works! Could you please explain the meaning of qualifiers though.
Could you also clarify this if possible:
replace(13 Jon Walker,'WAL') returns '13 Jon Walker' not 'WAL' But
replace(13 Jon Walker,'WAL') returns 'WAL' .
Why would that be?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by