【问题标题】:Need regex to remove spaces between multiple new lines characters需要正则表达式来删除多个换行符之间的空格
【发布时间】:2020-04-17 15:48:29
【问题描述】:

我正在处理一些 rawString,其中我需要从某些地方删除 \n,如果那里显示 \n,我需要在下一行删除一些地方。例如输入:-

 ** 由于冠状病毒的影响越来越重,LMT 将向其供应链中的中小型商业伙伴\n预付超过 5000 万美元,以保护就业,支持经济\n \n *新的生产线应该启动\n\正则 \n \n \n \n 文本。

我部分使用此代码得到结果

 String str = input.replaceAll("\\n","~").replaceAll("~\\s+",System.lineSeparator()+System.lineSeparator()).replaceAll("~", " ");

我现在得到的结果:

** 随着冠状病毒的影响加重,LMT 将向其供应链中的中小型商业伙伴预付超过 5000 万美元,以保护就业、支持经济


*新行应该开始正常





文本。

但问题是我连续获得不止一次“\n”,所有\n都替换为一个“\n”,因为此时我得到“\n”的空间,它会进入下一个行,例如,在这种情况下,它就像“\n \n 关键字”一样,“关键字”在下一行中显示正确,但在该问题上方创建了一个额外的行。任何人都可以请帮忙。

预期:-

** 随着冠状病毒的影响加重,LMT 将向其供应链中的中小型商业伙伴预付超过 5000 万美元,以保护就业、支持经济

*新行应该开始正常

文本。

换行输入模式

\n \n \n SomeText,\n\n SomeText\n,\n \n\n SomeText

在以上所有模式中,someText 将在下一行开始。

图案只会创造空间

\nSomeText\n\n\nSomeText\n\n\nSomeText

在所有这些情况下,只会创建空间" "

【问题讨论】:

  • 根据此编辑,您想删除`\ns`之间的所有空格并且只想保留\ns 对吗?
  • 是的,完全删除所有\ns之间的空格

标签: java android html string


【解决方案1】:

我建议进行以下更新:

String str = input
    .replaceAll("(\\n\\s+){2,}","~") // find duplicate linefeeds with optional spaces
    .replaceAll("\\n"," ") // replace remaining linefeeds with spaces
    .replaceAll("\\s{3,}", "") // remove redundant spaces
    .replaceAll("\\s{2}", " ") // replace duplicate spaces with one
    .replaceAll("~", System.lineSeparator() + System.lineSeparator() // restore linefeeds
);

它产生以下文本:

** As the coronavirus impact weighs, LMT to advance more than $50 mln to small, medium-sized business partners in its supply chain to protect jobs, support economy 

*New line should start regular 

text.

新测试用例的更新

String str = input
        .replaceAll("(\\n +\\n| \\n+ )","~") // mark duplicate linefeeds with optional spaces
        .replaceAll("\\n+"," ") // replace remaining linefeeds with spaces
        .replaceAll("(~\\s*)+","~") // remove duplicate linefeed marks with optional spaces
        .replaceAll("\\s{3,}", "") // remove redundant spaces
        .replaceAll("\\s{2}", " ") // replace duplicate spaces
        .replaceAll("~", System.lineSeparator() + System.lineSeparator()) // restore linefeeds
    ;
    System.out.println("[" + str + "]");

案例 2:“\n \n \n SomeText1, \n\n SomeText2\n,\n \n\n SomeText3”

[\n
\n
SomeText1,\n
\n
SomeText2 ,\n
\n
SomeText3]

案例 3:“\nSomeText1, \n\n\nSomeText2\n\n, \nSomeText3”

[ SomeText1, SomeText2 , SomeText3]

【讨论】:

  • 只有一个我看到我要进入下一行,只有在我得到“space+ \n +space”或“\n+\n+space”的地方,否则我显示单个空格。如果我想去下一行我得到 \n+space 或者如果“\n+space”不止一次,它会替换成一个并转到下一行。
  • 我在哪里得到这个“\n \n \n 但是”创建了一个额外的行
  • 对不起,我好像不太明白你的意思。当有三个或更多\n 字符时,是否要进行特殊处理?请更新您的测试和预期结果。
  • 更新请看懂
  • @Navjot.jassal 请检查更新的解决方案。所有测试通过。感谢您接受这个答案。
【解决方案2】:

如果我没记错的话,你想将多个\n 和空格转换成一个\n 然后

请尝试s = s.replaceAll("\\n\\s+\\b","\n");

对于这个特定的用例输出是

As the coronavirus impact weighs, LMT to advance more
than $50 mln to small, medium-sized business partners in its
supply chain to protect jobs, support economy 
New line should start
regular 
text.

一般情况

它将 \n\n \n \n , \n \n \n , \n \n\n \n \n 这些类型的模式转换为一个新行,即。 \n

【讨论】:

  • 不是那样,我只想在得到 \n+space 时才进入下一行,否则 \n 将替换为 " ",如果我得到 \n+space \n+space如果我想删除一个\n+空格
  • 我不太明白,即使没有空格,上述正则表达式也适用于多个\n,如果这不是您想要的,请提供示例模式及其预期替换,就像我在回答中提供的一样
  • 已更新请检查合理
  • 这个替换不会通过任何测试 :)
  • @AlexRudenko 你是对的,我测试不正确,我实际上对他的要求感到困惑,答案中的那个解决了他在问题标题中提出的问题,但现在他说他想保留\ns
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-21
  • 2021-01-24
相关资源
最近更新 更多