-1

I am currently trying to make use of the java string function someString.replaceAll() to find commonly used words (and, the, by, of, etc) and replace them with " ". Based on the answers to the question at Whitespace Matching Regex - Java, I produced this function call:

data.replaceAll("(?i)\\sthe\\s", " ")

However, it isnt working and I'm really not sure why. Nothing about it looks wrong based on what I've found. Please help me!

4
  • 3
    It isn't working - means? What output are you getting after that replace? Commented Jul 2, 2013 at 19:20
  • Meaning that it isn't replacing any of the words that I place in the regex with " ". It led me to assume that my regex wasnt matching the word, but I dont know why it isnt, since it looks right to me Commented Jul 2, 2013 at 19:22
  • commonly words without a pattern just use replace instead of replaceAll Commented Jul 2, 2013 at 19:23
  • Can you post complete code? I tried the following and it seems to replace 'The' string with space. For input 'me The rt', the code below prints 'me rt'. String data = "me The rt"; String result = data.replaceAll("(?i)\\sthe\\s", " "); System.out.println(result); Commented Jul 2, 2013 at 19:31

1 Answer 1

7

Strings are immutable!

data = data.replaceAll("(?i)\\sthe\\s", " ");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.