替换匹配正则表达式的子字符串
我获取一些 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
}
I fetch some html and do some string manipulation and en up with a string like
string sample = "\n \n 2 \n \n \ndl. \n \n \n flour\n\n \n 4 \n \n cups of \n\nsugar\n"
I would like to find all ingredient lines and remove whitespaces and linebreaks
2 dl. flour and 4 cups of sugar
My approach so far is to the following.
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
sample = sample.replaceAll("[\\n ]+", " ").trim();
输出:
2 dl.面粉 4 杯糖
开头没有空格,结尾也没有空格。
它首先用一个空格替换所有空格和换行符,然后从开头/结尾修剪掉多余的空格。
sample = sample.replaceAll("[\\n ]+", " ").trim();
Output:
2 dl. flour 4 cups of sugar
With no spaces in the beginning, and no spaces at the end.
It first replaces all spaces and newlines with a single space, and then trims of the extra space from the begging / end.
以下代码应该适合您:
输出
Following code should work for you:
OUTPUT
我认为这样的事情对你有用:
I think something like this will work for you:
我假设
\n
不是实际的换行符,但它也适用于linefeeds
。这应该可以正常工作:
test=test.replaceAll ("(?:\\s|\\\n)+"," ");
如果没有
文本 \n< /code> 它可以更简单:
test=test.replaceAll ("\\s+"," ");
您需要修剪前导/尾随空格。
我使用 RegexBuddy 工具来检查任何单个正则表达式,在多种语言中都非常方便。
I assumed that the
\n
are not actual line feed, but it also works withlinefeeds
.This should work fine :
test=test.replaceAll ("(?:\\s|\\\n)+"," ");
In case there is no
textual \n
it can be simpler:test=test.replaceAll ("\\s+"," ");
An you need to trim the leading/trailing spaces.
I use the RegexBuddy tool to check any single regex, very handy in so many languages.
您应该能够使用标准的 String.replaceAll(String, String)。第一个参数将采用您的模式,第二个参数将采用空字符串。
You should be able to use the standard String.replaceAll(String, String). The first parameter will take your pattern, the second will take an empty string.
运行这三个替换(将前导空格替换为空,将尾随空格替换为空,将多个空格替换为空格。
Run those three substitutions (replacing leading whitespace with nothing, replace trailing whitespace with nothing, replace multiple whitespace with a space.