Split a string into multiple lines using regular expressions with different capture groups

I am trying to split a string into several lines, but instead of using the string with only one simple regular expression pattern, I am trying to use a regular expression pattern that will split the string into different lines if it detects certain characters, but the characters are different. Instead of breaking a line into different lines, it gives me every single character in the specified line.

String[] splitstr = line.split("([&]{2})?([|]{2})?(!=)?"); 

Using the line above, I have a variable called line , which, for example, puts this line from a file:

 :if[input=right || input=Right]: 

I'm just wondering if there is a way to make this separation into

 ":if[input=right", "input=Right]:" 

And if I enclose the line as follows:

 :if[input=right || input=Right && input != null]: 

So that it breaks into

 ":if[input=right", "input=Right", "input != null]:" 

I used String#split(regex) for the character || , and it worked fine, but now that I want it to crash wherever there is || or && or != , and I want my code to be efficient and clean and easy to read.

+5
source share
2 answers

The java class below will split your sample string into parts of interest to you. The code will split the original string into all '||' and '& &' markup, globally. Ie if you have more than one '||' or '& &' operator in the source line, each part will be divided.

It should be noted that (\) special characters should be avoided. In Java, you also need to avoid escaping, so you need 2 backslashes to have a literal in your string.

Here's a great site for checking regEx code ... Regular Expressions 101

 public class splitStrRegex { public static void main(String[] args) { String myStr = ":if[input=right || input=Right && input != null]:"; String[] myStrings = myStr.split("\\|\\||&&"); for(int i = 0; i < myStrings.length; i++) { System.out.println(myStrings[i].trim()); } } } 

Output:

 :if[input=right input=Right input != null]: 
+2
source

This is a very interesting question. There are more answers on this site, for example: Java String.split () Regex Also, it is so easy to check, for example:

 public class Reg { public static void main(String args[]) { String string = "00!=4-0||34 & 55!=6"; String[] parts = string.split("[&||!=]"); for (int i = 0; i < parts.length; i++) { System.out.println("Part: " + parts[i]); } } } 

OUTPUT:

 Part: 00 Part: Part: 4-0 Part: Part: 34 Part: 55 Part: Part: 6 

But the problem is with the "& &" symbol, for example, for:

  String string = "00!=4-0||34 && 55!=6"; String[] parts = string.split("[&&||!=]"); 

OUTPUT:

 Part: 00 Part: Part: 4-0 Part: Part: 34 && 55 Part: Part: 6 

I can guess that we need to understand && in a different interpretation, for example:

 public class Reg { public static void main(String args[]) { String string = "00!=4-0||34 && 55!=6"; String[] parts = string.split("[&^&||!=]"); for (int i = 0; i < parts.length; i++) { System.out.println("Part: " + parts[i]); } } } 

OUTPUT:

 Part: 00 Part: Part: 4-0 Part: Part: 34 Part: Part: 55 Part: Part: 6 
0
source

Source: https://habr.com/ru/post/1269626/


All Articles