简体   繁体   English

如何从java字符串中获取子字符串,从下面给定字符串中的\\ n +到\\ n和\\ n-到\\ n的范围开始?

[英]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 . 在上面的字符串中,我想获取从\\ n +开始并以\\ n结尾的所有子字符串。 Also, starting from \\n- and ending with \\n using Java regular expression. 此外,从\\ n开始并使用Java正则表达式以\\ n结尾。

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. 实际上我想制作两个不同的集合,一个用于\\ n +到\\ n,另一个用于\\ n-到\\ n。 As I wanted to use this for later purposes. 因为我想将它用于以后的目的。

below is the java code I have tried with: 下面是我试过的java代码:

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. 从您提供的输出中,似乎还有其他条件也在您的问题中忘记提及,例如为什么###problem statement1不在输出中,因为它以\\n+开头并以新行结束。 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));
        }
    }
}

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

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