我想使用模式匹配从Java中的字符串中消除所有单个字母单词。我编码如下:使用java模式匹配去除单个字母单词
String str = "[email protected]";
//remove single char words and extra white spaces
inputStr = inputStr.replaceAll("\\b[\\w']{1}\\b", "").replaceAll("\\s+", " ").trim();
我期待输出为P @,因为输入不是单个字母的单词。但我得到的输出为@因为它消除了P.所以基本上它只考虑匹配模式的字母字符。而我想根据输入字符串的长度进行匹配。
请帮忙。
问题似乎是'\ b',它不会将@作为字母计数。 – brimborium