简体   繁体   中英

How to split a string with two empty lines in java using regex pattern for unix server

in windows following regex pattern is working: ("\\r\\n\\r\\n?\\n\\r") But I tried with forward slashes, but not working on server. Sample Data to split:

ABC
XYZ


NMB
YHJ

VGH

So, after splitting above data we want 2 arrays of string like

string[0] = ABC
XYZ
string[1] = NMB
YHJ

VGH

the line separator is different in different os. you should get line separator first.

System.getProperty("line.separator")

Ok, suppose we have a string and want to separate it:

String s = "ABC\nXYZ\n\n\nNMB\nYHJ\n\nVGH";
String separator = System.getProperty("line.separator");
String[] results = s.split(separator + separator);
System.out.println(results[0]);
// returns ABC
//         XYZ

Also you maybe want to split them again:

for (String res : results) {
    System.out.println(res.split(separator)[0]);
    // returns ABC
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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