I read this string from file:
abc | abc (abc\|abc)|def
I want to get array inludes 3 items:
How to write regex correctly?
line.split("(?!<=\\)\\|")
doesn't work.
Code:
public class __QuickTester {
public static void main (String [] args) {
String test = "abc|abc (abc\\|abc)|def|banana\\|apple|orange";
// \\\\ becomes \\ <-- String
// \\ becomes \ <-- In Regex
String[] result = test.split("(?<!\\\\)\\|");
for(String part : result) {
System.out.println(part);
}
}
}
Output:
abc
abc (abc\|abc)
def
banana\|apple
orange
Note: You need \\\\
(4 backslashes) to get \\
(2 backslashes) as a String, and then \\
(2 backslashes) becomes a single \
in Regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With