フィルターのクリア

How to replace a certain word in a plain text?

3 ビュー (過去 30 日間)
jojototo
jojototo 2017 年 1 月 23 日
コメント済み: jojototo 2017 年 2 月 4 日
Hi all, I have a text and I want to replace the word 'the' with 'a' ,I used regexpi but the problem is a word that contains the letters of "the" through it's letters also replaced....for example 'rather' becomes 'raar' ,can you please help me to solve this problem.

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 23 日
s = 'This is the string. The goal is to replace the thes with as';
newstr = regexprep(s, '\<the\>', 'a', 'preservecase');
This matches only words, replacing 'the' with 'a', and when 'The' is matched it replaces with 'A' (conserving case)
  2 件のコメント
Star Strider
Star Strider 2017 年 1 月 23 日
Brilliant!
jojototo
jojototo 2017 年 2 月 4 日
Thanks a lot ,it works ,this is brilliant

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 1 月 23 日
Use strrep() to replace space-the-space with space-a-space.
s = strrep(s, ' the ', ' a ');
  3 件のコメント
Image Analyst
Image Analyst 2017 年 1 月 23 日
What? How? What is your string? Look, here is an example that shows it works:
clc;
s = 'This is the string. The goal is to replace the thes with as'
s = strrep(s, ' the ', ' a ') % Replace lower case
s = strrep(s, ' The ', ' a ') % Replace upper case
Result:
s =
This is the string. The goal is to replace the thes with as
s =
This is a string. The goal is to replace a thes with as
s =
This is a string. a goal is to replace a thes with as
It looks like it works to me. What is your string?
jojototo
jojototo 2017 年 2 月 4 日
Thanks for your effort

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

カテゴリ

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