How to replace a certain word in a plain text?

조회 수: 2 (최근 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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by