manjunathのramigani:
私は、属性値のカップルが含まれている1つの文字列を持っています。文字列はいくつかの簡単な正規表現を使用して、特定の属性値が含まれているかどうかを確認しながら、マッチ関数は常にfalseを返しています。
今、私のような行動を必要とし、
- 文字列が含まれている場合は
\"import\" :
その後、私は必要なisExportSet
真として設定します。 - 文字列が含まれている場合は
\"path\" : true
その後、私は必要isPathSet
trueに設定します。
下図のように私が試したが、それは私のために動作しませんでした。
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、インポート=静的}
- 真偽