I want to remove certain words from a string. The words I want to remove are: “a”, “an”, “and”, “the”, “of”, and “or”.
I used the following method:
void doNoiseEliminator(Vector<String> input){
noNoiseLines = new Vector<String>();
String temp;
for(int i = 0; i < input.size(); i++) {
String regex = "(\\sand\\s)|(\\sa\\s)|(\\sthe\\s)|(\\san\\s)|(\\sof\\s)|(\\sor\\s)";
temp = input.get(i).replaceAll(regex, " ");
noNoiseLines.add(temp);
}
}
But this does not seem to work. My program takes a string line and circular shifts the line.
For the following input :
MY NAME IS JOHN
MY NAME IS AN A SAM
MY NAME IS OR RAW
Output is :
- a sam my name is
- is a sam my name
- is john my name
- is raw my name
- john my name is
- my name is a sam
- my name is john
- my name is raw
- name is a sam my
- name is john my
- name is raw my
- raw my name is
- sam my name is a
Why is this happening? How can I correct this? Please help me. Thanks…!!!
To be truthful I didnt understand your question fully, but try the simple way first, without regex, your problem might be there. Then go optimizing it if needed.
For example, try something like this.
void doNoiseEliminator(Vector input){
Of course this shouldnt be the final solution, its just to check if it works. Working, you can go towards checking/fixing the regex or any other solution.
Hope it helped to guide to the solution, cya.