• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replace ONLY "is" in a string with "is not"?  RSS feed

 
Sreelu Yeddanapudi
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to replace "is" in a string with "is not" :

return str.replaceAll("is","is not "); //

Input : Is it? Output : Is not it? //CORRECT
Input : This is? Output : This not is not //INCORRECT. Expecting : This is not

So I understood "is" should be preceded and followed by "zero or one white space character"
I tried return str.replaceAll("[\\s?is\\s?]","is not ");

Input : Is it? Output : Is not Is not Is not Is not tIs not ?
Input : This is? Output : This not is not is not is not is not //INCORRECT

Where am I going wrong? Please help
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall refer you to the Java Tutorials. \s*is will not work, because there are “zero” whitespace characters after th and before is in “this”.
Nor will \s+is because you can have is at the beginning of a line.
Try learning about word boundaries. That might work.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, square brackets have a special meaning -- and using it here changed the whole meaning of the regex.

Henry
 
Balaji Manoharan Bm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go. The below pattern works to some level...

str.replaceAll("(\\s+)(is)(?!\\w)","$1is not");

This works for me for following values
input: This is good output: This is not good
input: This is bad isn't it output: This is not bad isn't it
 
Balaji Manoharan Bm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have just found my above code fails if a word starts with 'is'. So added a ^is as an or condition.

str..replaceAll("^is|(\\s+)(is)(?!\\w)","$1is not");

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balaji Manoharan Bm wrote:have just found my above code fails if a word starts with 'is'. So added a ^is as an or condition.

str..replaceAll("^is|(\\s+)(is)(?!\\w)","$1is not");



As Campbell mentioned, Regular Expressions have support for word boundaries just for this. No need to use negative look-aheads, or alternations of options.

Henry
 
Balaji Manoharan Bm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. I have just over looked Campbell's comment.

Word boundaries make it so easy.

str.replaceAll("\\bis\\b","is not");
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreelu Yeddanapudi wrote:So I understood "is" should be preceded and followed by "zero or one white space character"

Are you sure about that? Because in the sentence "This is not", the "is" in the word "This" is also preceded and followed by zero or one white space character. (Preceded by zero, followed by one space character, to be specific).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!