简体   繁体   中英

How to fetch substring from java string starting with the range of \n+ to \n and \n- to \n in below given string?

Input string is as below:

@@ -106,12 +106,12 @@ end loop\n loop map dummm56\n \tdummy data/path/u/u_op/kl-sc45\n end loop\n-\n-loop map {$df=56,$kl=20564300,$testId=\"jk: message1 message2:48667697\",$kl3=true,$kl=true, $kl=[2],$kl=$kl1, $kl1=true, $kl1=[1],$kl14=$kl15,$kl16=$kl14}\n+##### message2:48667697\n+loop map {val56}\n \tl1 l2/l3/i3/l7_l90/l90-SC21_l90/l90-l90_l90_l90\n end loop\n-\n-loop map {val56}\n+#####kl message45:48667697\n+loop map {val34}\n \ttestcases data/testcases/path1/[ath5+path6/UC20015-SC21/UC20015-SC21\n end loop\n

In above string, I want to fetch all the sub-strings starting from \\n+ and ending with \\n . Also, starting from \\n- and ending with \\n using Java regular expression.

Expected Output is as below:

blank  // here its blank as first \n- to next \n nothing is there.
loop map {$df=56,$kl=20564300,$testId=\"jk: message1 message2:48667697\",$kl3=true,$kl=true, $kl=[2],$kl=$kl1, $kl1=true, $kl1=[1],$kl14=$kl15,$kl16=$kl14} // as second \n- to next \n
##### message2:48667697 //third \n+ to next \n
loop map {val56}\n \tl1 l2/l3/i3/l7_l90/l90-SC21_l90/l90-l90_l90_l90 //fourth \n+ to next \n
blank // \n- to next \n
loop map {val56} // \n- to next \n
#####kl message45:48667697 // as \n+ to \n
loop map {val34} // as \n+ to \n

Actually I wanted to make two different sets, one for \\n+ to \\n and another for \\n- to \\n. As I wanted to use this for later purposes.

below is the java code I have tried with:

String str="Pasted above string making sure no additional string literals should come.";
Pattern p0 = Pattern.compile("^(\\\\n[+|-])(.*?)(\\\\n.*)?$"); 
Matcher m = p0.matcher(str);
while (m.find()) {
System.out.printf( m.group(0));// here I am expecting my output to get printed.
}

Can anyone help me out with the same. Any help would be highly appreciated. Thanks.

This is the regex that might help you

^(\\\\\\\\n[+|-])(.*)$

^ - Start of line

\\\\\\\\n - escaping \\

^(\\\\\\\\n[+|-]) - Starting point of line followed by \\n with either + or -

(.*?) anything can follow after that (? for non-greedy search if \\n follows after that)

(\\\\\\\\n.*)? - Might be that \\n follows (4th line in text)

$ - End of line. Assuming that after end of line what follows is \\n-new line

From the output that you have provided, it seems that there are other conditions also that you have forgot to mention in your question, like why ###problem statement1 is not in the output because it starts with \\n+ and ends with new line. Below is the code snippet for the same

 public static void main(String[] args) throws Exception {
    Pattern pattern = Pattern.compile("^(\\\\n[+|-])(.*?)(\\\\n.*)?$"); 
    Matcher matcher = null;

    BufferedReader br = new BufferedReader(new FileReader(
            "filePath"));
    String line = null;
    while ((line = br.readLine()) != null) {
        matcher = pattern.matcher(line);
        while (matcher.find()) {
            System.out.println(matcher.group(2));
        }
    }
}

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