我获取了一些HTML并进行了一些字符串操作,最终得到了一个类似于以下的字符串:
string sample = "\n \n 2 \n \n \ndl. \n \n \n flour\n\n \n 4 \n \n cups of \n\nsugar\n"
我希望找到所有的配料并去除空格和换行符。
2 dl面粉 和 4杯糖
目前我的方法如下。
Pattern p = Pattern.compile("[\\d]+[\\s\\w\\.]+");
Matcher m = p.matcher(Result);
while(m.find()) {
// This is where i need help to remove those pesky whitespaces
}
replaceAll()
呢? - Alan MoorereplaceAll()
方法,但是原始问题提出者想要使用Pattern/Matcher类来实现,所以编写了相应的代码。 - anubhava