10

I'm trying to remove some words in a string using regex using below program. Its removing properly but its considering only case sensitive. How to make it as case insensitive. I kept (?1) in replaceAll method but it didn't work.

package com.test.java;

public class RemoveWords {

    public static void main(String args[])
    {

        // assign some words to string

        String sample ="what Is the latest news today in Europe? is there any thing special or everything is common.";

            System.out.print(sample.replaceAll("( is | the |in | any )(?i)"," "));
    }
}

OUTPUT:

what Is latest news today  Europe? there thing special or everything common.

3 Answers 3

33

You need to place the (?i) before the part of the pattern that you want to make case insensitive:

System.out.print(sample.replaceAll("(?i)\\b(?:is|the|in|any)\\b"," "));
                                    ^^^^

See it

I've replaced spaces around the keywords to be removed with word boundary (\\b). The problem comes because there may be two keywords one after another separated by just one space.

If you want to delete the keywords only if they are surrounded by space, then you can use positive lookahead and lookbehind as:

(?i)(?<= )(is|the|in|any)(?= )

See it

Sign up to request clarification or add additional context in comments.

8 Comments

"the" exists in the output. it didn't replace with " " and it doesn't remove "IS" see it ideone.com/HMxLr
Correct. Why it's not replacing "the" with " " . Any problem with the regex
@codaddict: Note that this leaves multiple spaces in a row, which is possibly not ideal but may be tricky to avoid.
@codaddict works but I don't understand anything from this regex. Anyway, I revert my -1
@Jon: You are right. But adding a .replaceAll(" +"," ") will solve that. Doing all this in a single regex will be tricky.
|
4

I don't think you can specify case insensitive with the quick replace. Try a pattern instead. i.e:

package com.test.java;

public class RemoveWords {

public static void main(String args[]) {
  // assaign some words to string
  String sample ="what Is the latest news today in Europe? is there any thing special or everything is common.";
  String regex = "( is | the |in | any )"
  System.out.print
  (
    Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(sample).replaceAll("")
  );
 }
}

2 Comments

Sorry for the downvote, but as @codaddicts answer shows, you can use those flags in String.replaceAll().
@Joachim: No worries. I upvoted @Codaddicts answer since I got to know how to use flags in String.replaceAll
1

change is to [iI][sS]

sample.replaceAll("( [iI][sS] ...

1 Comment

It works but lengthy process where we need to put every character. Is there any better way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.