Java regex: newline + white space

人盡茶涼 提交于 2019-12-01 14:46:44

问题


should be simple, but I'm going crazy with it.

Given a text like:

line number 1
line number 2
 line number 2A
line number 3
 line number 3A
 line number 3B
line number 4

I need the Java regex that deletes the line terminators then the new line begin with space, so that the sample text above become:

line number 1
line number 2line number 2A
line number 3line number 3Aline number 3B
line number 4

回答1:


yourString.replaceAll("\n ", " "); this wont help?




回答2:


String res = orig.replaceAll("[\\r\\n]+\\s", "");



回答3:


Perhaps to make it cross-platform:

String pattern = System.getProperty("line.separator") + " ";
string.replaceAll(pattern, "");



回答4:


"\n " This is should do the trick if you are in Unix LF mode. For DOS like you need to match CRLF "\r\n ". Did check with RegexBuddy looking fine.



来源:https://stackoverflow.com/questions/6244362/java-regex-newline-white-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!