Java文字列の検索パターンの問題(/マッチが含まれています)

manjunathのramigani:

私は、属性値のカップルが含まれている1つの文字列を持っています。文字列はいくつかの簡単な正規表現を使用して、特定の属性値が含まれているかどうかを確認しながら、マッチ関数は常にfalseを返しています。

今、私のような行動を必要とし、

  1. 文字列が含まれている場合は\"import\" :その後、私は必要なisExportSet真として設定します。
  2. 文字列が含まれている場合は\"path\" : trueその後、私は必要isPathSettrueに設定します。

下図のように私が試したが、それは私のために動作しませんでした。

public class DriverClass {
    public static void main(String[] args) {
        String str = "\"import\" : \"static\",\"path\" : true";
        boolean isExportSet = str.matches("\\*+export\\*+");
        boolean isPathSet = str.matches("\\*+multipath\\s+:\\s+true");

        System.out.println("Export " + isExportSet);
        System.out.println("Path  " + isPathSet);
    }
}
別のコーダ:

次のコードは、問題のdeifinitionを満たす場合は私に知らせてください。

static String str = "\"import\" : \"static\",\"path\" : true";

static void test(String str) {
    Map<String, String> map = new HashMap<String, String>();
    String[] parts = str.split("(:|,)");
    for (int i = 0; i < parts.length - 1; i+=2) {
        map.put(getUnquotedStr(parts[i]), getUnquotedStr(parts[i+1]));
    }
    System.out.println(map.size() + " entries: " + map); // 2 entries: {path=true, import=static}
    boolean isExportSet = "".equals(map.get("import"));
    boolean isPathSet = "true".equals(map.get("path"));
    System.out.println(isExportSet + " - " + isPathSet);
}

private static String getUnquotedStr(String str) {
    return str.replaceAll("\"", "").trim();
}

コンソールに次のように出力されます:

  • 2つのエントリ:{パス= TRUE、インポート=静的}
  • 真偽

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=219555&siteId=1