I have the following string:

```
String input = "Remove from em?ty sentence 1? Remove from sentence 2! But not from ip address 190.168.10.110!";

```

I want to remove punctuation marks at the right places. My output needs to be:

```
String str = "Remove from em?ty sentence 1 Remove from sentence 2 But not from ip address 190.168.10.110";

```

I am using the following code:

```
while (stream.hasNext()) { 
    token = stream.next();
    char[] tokenArray = token.toCharArray();
    token = token.trim();

    if(token.matches(".*?[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}[\\.\\?!]+")){
        System.out.println("case2");
        stream.previous();
        int len = token.length()-1;
        for(int i = token.length()-1; i>7; i--){
            if(tokenArray[i]=='.'||tokenArray[i]=='?'||tokenArray[i]=='!'){
                --len;
            }
            else
                break;
        }
        stream.set(token.substring(0, len+1));
    }
    else if(token.matches(".*?\\b[a-zA-Z_0-9]+\\b[\\.\\?!]+")){
        System.out.println("case1");
        stream.previous();
        str = token.replaceAll("[\\.\\?!]+", "");
        stream.set(str);

        System.out.println(stream.next());                          
    }
}

```

'Tokens' are getting sent from the 'input' string. Can you please indicate what i am doing wrong in terms of regex or the logic?

A punctuation is considered one when it ends a sentence, is not present within an ip address, not within words such as `!true`, `emp?ty` (leave them alone). Also may be followed by a space or end of string.