正则匹配字符串问题?

HashMap<String, String> base = new HashMap<>();
base.put("!",  "0");
base.put("@",  "1");
base.put("#",  "2");
base.put("$",  "3");
base.put("%",  "4");
base.put("^",  "5");
base.put("&",  "6");
base.put("*",  "7");
base.put("(",  "8");
base.put(")",  "9");
base.put("|",  "/");
base.put("{",  "+");
base.put("}",  "=");
 
字符串 "TV/RJek16S+T0=" -> 替换成 "TV|RJek@&S{T!}"
反转   "TV|RJek@&S{T!}" -> "TV/RJek16S+T0="
阅读 2k
2 个回答

不用正则,用替换的方法

let base = {
  "!": "0",
  "@": "1",
  "#": "2",
  "$": "3",
  "%": "4",
  "^": "5",
  "&": "6",
  "*": "7",
  "(": "8",
  ")": "9",
  "|": "/",
  "{": "+",
  "}": "="
};
let str = "TV/RJek16S+T0=";
Object.entries(base).forEach(([key,val])=>{
    str = str.replaceAll(val,key);
})
console.log(str);//TV|RJek@&S{T!}
let str = "TV|RJek@&S{T!}";
Object.entries(base).forEach(([key,val])=>{
    str = str.replaceAll(key,val);
})
console.log(str);//TV/RJek16S+T0=

ps 感觉像作业

直接替换就行了

推荐问题