Split string in Java to show only character sequence

I am trying to split a line like the line below

3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4 

to a row table that does not have a number or sign.

It means

 a[0]=x a[1]=y a[2]=x a[3]=w 

I tried this

 split("(\\+|\\-|\\d)+\\d*") 

but it does not seem to work.

+4
source share
6 answers

The following should work:

 String[] letters = input.split("[-+\\d]+"); 
+5
source

Edit: -

If you want xw be together in the resulting array, you need to split the line: -

 String[] arr = str.split("[-+\\d]+"); 

Result: -

 [, x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z] 

You can replace all unnecessary characters with an empty string and split into an empty string.

 String str = "3x2y3+5x2w3-8x2w3z4+3-2x2w3+9y-4xw-x2x3+8x2w3z4-4"; str = str.replaceAll("[-+\\d]", ""); String[] arr = str.split(""); System.out.println(Arrays.toString(arr)); 

Note that this will add an empty string as the first element of your array that you can handle.

Result: -

 [, x, y, x, w, x, w, z, x, w, y, x, w, x, x, x, w, z] 

Please note that the - sign in your question is different. You must replace it with a keyboard. It currently does not match the - sign.

+3
source

This single line file does everything:

 String[] letters = input.replaceAll("(^[^az]*)|([^az]*$)", "").split("[^az]+"); 

This also applies to leading / trailing characters, so you don't get empty elements at the beginning of the array (e.g. some other answers)

Test with a line:

 public static void main(String[] args) { String input = "3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4"; String[] letters = input.replaceAll("(^[^az]*)|([^az]*$)", "").split("[^az]+"); System.out.println(Arrays.toString(letters)); } 

Output:

 [x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z] 

Note that in the array

there is no leading "empty" element,
+1
source

note - and - not the same code, one just ascii minus another long (encoded UTF8 e28093)

 public class Test { public static void main(String pArgs[]) { String s="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4"; String splitreg="(\\+|\\-|\\d|–)+\\d*"; if ( pArgs.length > 0 ) { splitreg=pArgs[0]; } System.out.println("splitting '" + s + "' with '" + splitreg + "'"); String[] splitted=s.split(splitreg); for (int i=0; i < splitted.length; i++ ) { System.out.println("["+ i + "]" + "=" + splitted[i]); } } } 

/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java Test

 splitting '3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4' with '(\+|\-|\d|–)+\d*' [0]= [1]=x [2]=y [3]=x [4]=w [5]=x [6]=w [7]=z [8]=x [9]=w [10]=y [11]=xw [12]=x [13]=x [14]=x [15]=w [16]=z 
0
source
 String[] letters = input.split("[\\d\\+\\-]+"); 
0
source

Is this what you are trying to achieve?

  String data="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4"; //lets replace all unnecessary elements with spaces data=data.replaceAll("[-+–\\d]", " "); // now string looks like: // " xyxwxwzxwy xw xxxwz " // lets remove spaces from start and end data=data.trim(); // data looks like: // "xyxwxwzxwy xw xxxwz" // and split in places where is at least one space String[] arr=data.split("\\s+"); System.out.println(Arrays.toString(arr)); 

Output:

 [x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z] 
0
source

All Articles