替换匹配正则表达式的子字符串

发布于 2024-11-10 02:56:30 字数 477 浏览 3 评论 0原文

我获取一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泛滥成性 2024-11-17 02:56:30

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.

两仪 2024-11-17 02:56:30

以下代码应该适合您:

String sample = "\n    \n   2 \n      \n  \ndl. \n \n    \n flour\n\n     \n 4   \n    \n cups of    \n\nsugar\n";
Pattern p = Pattern.compile("(\\s+)");
Matcher m = p.matcher(sample);
sb = new StringBuffer();
while(m.find())
    m.appendReplacement(sb, " ");
m.appendTail(sb);
System.out.println("Final: [" + sb.toString().trim() + ']');

输出

Final: [2 dl. flour 4 cups of sugar]

Following code should work for you:

String sample = "\n    \n   2 \n      \n  \ndl. \n \n    \n flour\n\n     \n 4   \n    \n cups of    \n\nsugar\n";
Pattern p = Pattern.compile("(\\s+)");
Matcher m = p.matcher(sample);
sb = new StringBuffer();
while(m.find())
    m.appendReplacement(sb, " ");
m.appendTail(sb);
System.out.println("Final: [" + sb.toString().trim() + ']');

OUTPUT

Final: [2 dl. flour 4 cups of sugar]
心如狂蝶 2024-11-17 02:56:30

我认为这样的事情对你有用:

String test = "\n    \n   2 \n      \n  \ndl. \n \n    \n flour\n\n     \n 4   \n    \n cups of    \n\nsugar\n";

/* convert all sequences of whitespace into a single space, and trim the ends */
test = test.replaceAll("\\s+", " ");

I think something like this will work for you:

String test = "\n    \n   2 \n      \n  \ndl. \n \n    \n flour\n\n     \n 4   \n    \n cups of    \n\nsugar\n";

/* convert all sequences of whitespace into a single space, and trim the ends */
test = test.replaceAll("\\s+", " ");
朕就是辣么酷 2024-11-17 02:56:30

我假设 \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 with linefeeds.
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.

丘比特射中我 2024-11-17 02:56:30

您应该能够使用标准的 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.

野味少女 2024-11-17 02:56:30
s/^\s+//s
s/\s+$//s
s/(\s+)/ /s

运行这三个替换(将前导空格替换为空,将尾随空格替换为空,将多个空格替换为空格。

s/^\s+//s
s/\s+$//s
s/(\s+)/ /s

Run those three substitutions (replacing leading whitespace with nothing, replace trailing whitespace with nothing, replace multiple whitespace with a space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文