I'm new to regex, What i'm trying to do is to split the String with certain conditions but i don't know how to do it.
Here is the sample/condition
String str = sum1=x+(5+a)
Array[0] = sum1
Array[1] = =
Array[2] = x
Array[3] = +
Array[4] = (
Array[5] = 5
Array[6] = +
Array[7] = a
Array[8] = )
I want the string to be split by +,-,*,\,(,),=
Is this possible?
解决方案
If you really need to use regex you should probably use look-around mechanism since you don't want to split at this characters, but before or after them.
String str = "sum1=x+(5+a)";
String[] array=str.replaceAll("\\s+","")//remove all spaces
.split("(?<=[+\\-*\\\\()=/])|(?=[+\\-*\\\\()=/])");
for (int i=0; i
System.out.println("Array["+i+"] = "+array[i]);
output:
Array[0] = sum1
Array[1] = =
Array[2] = x
Array[3] = +
Array[4] = (
Array[5] = 5
Array[6] = +
Array[7] = a
Array[8] = )