0

ファイル (2 列) をreadLine();に分割する方法を見つけようとしています。多くの区切り文字を考慮することによって (以下を参照)。

これが私の区切り文字のすべての可能性です(コメントを参照)

+--------+---------+
+ ##some text      + //some text which starts with (##) I want to exclude this row
+ 341,     222     + //comma delimited
+ 211      321     + //space delimited
+ 541      1231    + //tab delimited
+ ##some text      + //some text which starts with (##) I want to exclude this row
+ 11.3     321.11  + //double values delimited by tab
+ 331.3    33.11   + //double values delimited by space
+ 231.3,   33.1    + //double values delimited by comma
+ ##some text      + //some text which starts with (##) I want to exclude this row
+--------+---------+

このテーブルを取得したい:

+--------+---------+
+ 341        222   + 
+ 211        321   +
+ 541        1231  +
+ 11.3      321.11 +
+ 331.3     33.11  +
+ 231.3      33.1  +
+--------+---------+

この問題の解決策を見つけて喜んでいます

アップデート:

今のところ、私は([,\s\t;])+ (コンマ、タブ、スペース、セミコロン...) を持っていますが、##some テキストに対して行う方法がわかりません。\##\w+ を試しましたが、うまくいきませんでした。何かアドバイス?

4

2 に答える 2

1

これを試すことができます...
私はそれを試してみましたが、うまく機能しています。

(\\d+\\.?\\d*),?\\s*?(\\d+\\.?\\d*)

と に置き換え$1ます$2

編集:

以下のコードを試してください...

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class regcheck
{
    private static Pattern twopart = Pattern.compile("(\\d+\\.?\\d*),?\\s*?(\\d+\\.?\\d*)");

    public static void checkString(String s)
    {
        Matcher m = twopart.matcher(s);
        if (m.matches()) {
            System.out.println(m.group(1) +" " + m.group(2));
        } else {
            System.out.println(s + " does not match.");
        }
    }

    public static void main(String[] args) {
        System.out.println("Parts of strings are ");
        checkString("##some text");
        checkString("123,     4567");
        checkString("123,   342");
        checkString("45.45   4.3");
        checkString("3.78,  23.78");

  }  
}

出力:

Parts of strings are
##some text does not match.
123 4567
123 342
45.45 4.3
3.78 23.78

m.group(1)最初の部分を提供します。
m.group(2)第二部をお届けします。

あなたのコードではcheckstring()、単一行のメソッドを使用しています....

于 2012-11-22T06:41:30.487 に答える
0

ASCII が入力の一部ではないと仮定すると、これを試すことができます。

##[a-z\s]+|([\d\.]+)[,\s\t]+([\d\.]+)

次に、次のように置き換えます。

\1   \2     (or $1    $2)

これは、数字にコンマを使用できないことに注意してください

于 2012-11-21T12:09:02.930 に答える