我有以下字符串:
String input = "Remove from em?ty sentence 1? Remove from sentence 2! But not from ip address 190.168.10.110!";
我想删除正确位置的标点符号。我的输出需要是:
String str = "Remove from em?ty sentence 1 Remove from sentence 2 But not from ip address 190.168.10.110";
我正在使用以下代码:
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());
}
}
“令牌”是从“输入”字符串发送的。您能否指出我在正则表达式或逻辑方面做错了什么?
标点符号在句子结束时被视为一个标点符号,它不存在于 IP 地址中,也不存在于诸如 , 之类的单词中!true
(emp?ty
不要理会它们)。也可以后跟空格或字符串结尾。