どのように私は、文字列が私のパターンに一致すると言うことができます

Mohadeseh:

私はそれがこのパターンと私の文字列一致であると言うことができる方法:名前とバージョンは、ホワイトスペースのない任意の文字列を指定できます(OSの#NAME #versionをインストールします)。

サミュエルシルバーモース:

あなたは、次のコードを使用することができたよう。

    // assuming parenthesis an sharp are pattern included:
    String s = "(install OS #testname #testversion)";
    if (s.matches("\\(install\\sOS\\s#\\S+\\s#\\S+\\)")) {
        String[] splitted = s.split("\\s");
        String name = splitted[2].replace("#", "");
        String version = splitted[3].replace(")", "").replace("#", "");
        System.out.println("name: " + name);
        System.out.println("version: " + version);
    }
    // assuming parenthesis an sharp are both not pattern included:
    s = "install OS testname2 testversion2";
    if (s.matches("install\\sOS\\s\\S+\\s\\S+")) {
        String[] splitted = s.split("\\s");
        String name = splitted[2];
        String version = splitted[3];
        System.out.println("name: " + name);
        System.out.println("version: " + version);
    }

(あなたは部品間mutliple空白を許可する必要がある場合、あなたは両方に置き換える必要があり、分割された正規表現と一致する正規表現、\ sの\ S +)

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=374953&siteId=1