简体   繁体   English

需要正则表达式来删除多个换行符之间的空格

[英]Need regex to remove spaces between multiple new lines characters

I am working on some rawString in which i need to remove \n from some places and Some where i need to go in next line if there showing \n.For Example Input:-我正在处理一些 rawString,我需要从某些地方删除 \n,如果显示 \n,我需要在下一行删除 go。例如输入:-

 ** As the coronavirus impact weighs, LMT to advance more\nthan $50 mln to small, medium-sized business partners in its\nsupply chain to protect jobs, support economy \n  \n  *New line should start\n\nregular \n  \n \n  \n text.

I am getting the result Partially using this code我部分使用此代码得到结果

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

Result right now i am getting:结果现在我得到:

** 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 startregular 





text.

but problem is where i am getting more than one time "\n" consecutively, that all \n replace into one "\n", because at this time where i am getting "\n" with space its going to next line, e,g so it like "\n \n Keyword" in this case "Keyword" is showing in next line that correct but creating one extra line above that a problem.但问题是我连续得到不止一次“\n”,所有\n都替换为一个“\n”,因为此时我得到“\n”的地方,它带有空格,它进入下一行,e ,g 所以它就像 "\n \n Keyword" 在这种情况下 "Keyword" 在下一行显示正确但在该问题上方创建一个额外的行。 Can anyone please help on this.任何人都可以请帮忙。

Expected:-预期的:-

** 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 startregular 

text.

Input Pattern for New line新行的输入模式

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

In all above Pattern someText will start in next line.在上述所有模式中, someText将从下一行开始。

Pattern will create space only图案只会创造空间

\nSomeText , \n\n\nSomeText\n\n , \nSomeText \nSomeText , \n\n\nSomeText\n\n , \nSomeText

in these all cases will create only space " " ,在这些情况下,只会创建空间" "

I suggest the following update:我建议进行以下更新:

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
);

It produces the following text:它产生以下文本:

** 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.

Update for new test cases更新新的测试用例

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 + "]");

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

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

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

[ SomeText1, SomeText2 , SomeText3]

If i didn't get it wrong you want to convert the multiple \n and spaces in one \n then如果我没有弄错,您想将多个\n和空格转换为一个\n然后

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

for this particular use case output was对于这个特殊的用例 output 是

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.

In general一般来说

It will convert \n\n \n \n , \n \n \n , \n \n\n \n \n these kinds of patterns into a single new line ie.它将\n\n \n \n\n \n \n\n \n\n \n \n这些类型的模式转换为一个新行,即。 \n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM