I am trying to split a string into multiple strings but rather than using a string just one simple regex pattern, I am trying to use a regex pattern that will split a string into different strings if it detects certain characters, but the characters are different. Instead of splitting the string into different strings, it's giving me each and every individual characters in said string.
我试图将一个字符串拆分成多个字符串,而不是只使用一个字符串只是一个简单的正则表达式模式,我试图使用一个正则表达式模式,如果它检测到某些字符,将字符串拆分为不同的字符串,但字符是不同的。它不是将字符串拆分成不同的字符串,而是为我提供所述字符串中的每个字符。
String[] splitstr = line.split("([&]{2})?([|]{2})?(!=)?");
Using the above line, I have a variable called line
which, as an example, I am putting this line from a file:
使用上面这一行,我有一个名为line的变量,作为一个例子,我从文件中放入这一行:
:if[input=right || input=Right]:
I am just wondering if there is a way to make this split into
我只是想知道是否有办法让这种分裂
":if[input=right", "input=Right]:"
And if I put in a line like this:
如果我输入这样一行:
:if[input=right || input=Right && input != null]:
So that it splits into
所以它分裂成
":if[input=right", "input=Right", "input != null]:"
I was using String#split(regex)
for the ||
symbol and it worked just fine, but now that I want it to split wherever that are ||
or &&
or !=
and I want my code to be efficient and clean and easy to read.
我正在使用String#split(regex)作为||符号,它工作得很好,但现在我希望它分开在哪里||或&&或!=我希望我的代码高效,干净,易于阅读。
2 个解决方案
#1
2
The java class below will split your example string into the parts you are interested in. The code will split the original string at all '||' and '&&' delimeters, globally. I.e, if you have more than one '||' or '&&' operator in your original string, each part will be split out.
下面的java类将您的示例字符串拆分为您感兴趣的部分。代码将原始字符串拆分为“||”和'&&'分界,全球。即,如果你有多个'||'或原始字符串中的'&&'运算符,每个部分将被拆分。
One thing to note is the need to escape (\) the special characters. In Java you also need to escape the escape, so you need 2 backslashes in order to have a literal in your string.
需要注意的一件事是需要转义(\)特殊字符。在Java中,您还需要转义转义,因此您需要2个反斜杠才能在字符串中包含文字。
Here's a great site to test out regEx code ... Regular Expressions 101
这是一个测试regEx代码的好网站...正则表达式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
0
It is a very interesting question. There are more answers for that on the site like: Java String.split() Regex Also, it so easy to check, like:
这是一个非常有趣的问题。在网站上有更多的答案,如:Java String.split()Regex此外,它很容易检查,如:
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 problem with symbol "&&", for example, for the:
但是符号“&&”的问题,例如,对于:
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 other 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
#1
2
The java class below will split your example string into the parts you are interested in. The code will split the original string at all '||' and '&&' delimeters, globally. I.e, if you have more than one '||' or '&&' operator in your original string, each part will be split out.
下面的java类将您的示例字符串拆分为您感兴趣的部分。代码将原始字符串拆分为“||”和'&&'分界,全球。即,如果你有多个'||'或原始字符串中的'&&'运算符,每个部分将被拆分。
One thing to note is the need to escape (\) the special characters. In Java you also need to escape the escape, so you need 2 backslashes in order to have a literal in your string.
需要注意的一件事是需要转义(\)特殊字符。在Java中,您还需要转义转义,因此您需要2个反斜杠才能在字符串中包含文字。
Here's a great site to test out regEx code ... Regular Expressions 101
这是一个测试regEx代码的好网站...正则表达式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
0
It is a very interesting question. There are more answers for that on the site like: Java String.split() Regex Also, it so easy to check, like:
这是一个非常有趣的问题。在网站上有更多的答案,如:Java String.split()Regex此外,它很容易检查,如:
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 problem with symbol "&&", for example, for the:
但是符号“&&”的问题,例如,对于:
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 other 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