4

以下の文字列のような文字列を分割しようとしています

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

数字や記号を持たない文字列のテーブルに。

つまり

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

私はこれを試しました

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

しかし、うまくいかないようです。

4

6 に答える 6

5

以下が機能するはずです。

String[] letters = input.split("[-+\\d]+");
于 2012-12-19T20:17:44.160 に答える
3

編集: -

結果の配列で一緒にしたい場合はxw、文字列を分割する必要があります: -

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

出力: -

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

不要な文字をすべて空の文字列に置き換え、空の文字列で分割できます。

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));

これにより、配列の最初の要素として空の文字列が追加されることに注意してください。これは処理できます。

出力: -

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

-質問へのサインインは異なることに注意してください。キーボード上のものと交換する必要があります。現在、-符号が一致していません。

于 2012-12-19T20:17:25.160 に答える
1

このワンライナーはそれをすべて行います:

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

これは先頭/末尾の文字も処理するため、配列の先頭に空白の要素を取得しません(他の回答のように)

あなたの文字列を使ったテスト:

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

出力:

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

配列に先頭の「空白」要素がないことに注意してください

于 2012-12-19T20:14:49.917 に答える
0

備考 - と – は同じコードではありません。1 つは ascii からもう 1 つは long です (コード化された 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 テスト

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
于 2012-12-19T20:23:33.157 に答える
0

これはあなたが達成しようとしていることですか?

 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:
 // " x y   x w   x w z     x w   y  xw x x   x w z   "

 // lets remove spaces from start and end
 data=data.trim();
 // data looks like:
 // "x y   x w   x w z     x w   y  xw x x   x w z"

 // and split in places where is at least one space
 String[] arr=data.split("\\s+");

 System.out.println(Arrays.toString(arr));

出力:

[x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]
于 2012-12-19T20:52:02.783 に答える
0
String[] letters = input.split("[\\d\\+\\-]+");
于 2012-12-19T20:25:42.370 に答える