繁体   English   中英

正则表达式删除访问令牌模式并替换为空字符串

[英]Regex to remove the access token pattern and replace with empty string

我正在尝试从 JSON 响应中查找并删除访问令牌模式。

{
"name":"any name",
"access_token":"xxx-xxxxx-1234-45rf",
"some_data":"some random data",
"Api_key":"1234-cd34-xxx-xxxx"
}

我正在尝试使用下面的正则表达式

".*-.*-.*"

但它正在从字符串中删除整行,例如

"access_token":"xxx-xxxxx-1234-45rf",

来自 JSON。

我想要达到的结果如下。

{
"name":"any name",
"access_token":"",
"some_data":"some random data",
"Api_key":""
}

上面的 access_token 只是一个虚拟的,实际的密钥可以是带有连字符的不同长度的字符串。

您可以在replaceAll函数中使用正则表达式。 这将用空字符串替换acces_token及其值。

String jsonString = "{ \"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\" }";

jsonString.replaceAll("(\"access_token\":)(\")(.*?)(\",) ", "")

尝试这个:

String json = "{ \"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\" }";

json = json.replaceAll(".{3}-.{5}-[\\d]{4}-[\\w]{4}", "");

看到它在这里工作

JAVA

导入所需的包

import java.util.regex.Matcher;
import java.util.regex.Pattern;
❶ json 字符串
public static void main(String[] args) {
    Pattern p = Pattern.compile(":\\\"(.*?)\\\"");
    String json = "{\"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\"}";
    Matcher m = p.matcher(json);
    StringBuffer buf = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(buf, String.format(":\"%s\"", m.group(1)).replaceAll(".*-.*-.*", ":\"\""));
    }
    m.appendTail(buf);
    System.out.println(buf); // output: {"name":"any name", "access_token":"", "some_data":"some random data", "Api_key":""}
}
❷ 静态方法
private static String removeToken(String content) {
    Pattern p = Pattern.compile(".*-.*-.*");
    return p.matcher(content).replaceAll("");
}

JavaScript

❶ 没有嵌套对象

 const obj = { "name": "any name", "access_token": "xxx-xxxxx-1234-45rf", "some_data": "some random data", "Api_key": "1234-cd34-xxx-xxxx" }; for (const [key, value] of Object.entries(obj)) { obj[key] = value.replace(/.*-.*-.*/, ''); } console.log(obj);

❷ 多级嵌套对象

 const obj = { "name": "any name", "access_token": "xxx-xxxxx-1234-45rf", "some_data": "some random data", "Api_key": "1234-cd34-xxx-xxxx", "obj": { "111name": "any name", "access_token": "xxx-xxxxx-1234-45rf", "some_data": "some random data", "Api_key": "1234-cd34-xxx-xxxx" } }; const restructure = obj => { return Object.keys(obj).reduce((data, key) => { let value = obj[key]; if (typeof value === 'string') { value = value.replace(/.*-.*-.*/, ''); } else if (typeof value === 'object') { value = restructure(value) } data[key] = value; return data }, {}) } console.log(restructure(obj));

您需要的代码是:

String json = "{ \"name\":\"any name\", \"access_token\":\"xxx-xxxxx-1234-45rf\", \"some_data\":\"some random data\", \"Api_key\":\"1234-cd34-xxx-xxxx\" }";
json = json.replaceAll("(\"access_token\")\\s*:\\s*\"[^\"]+\"", "$1:\"\"");

正则表达式选取带引号的项目名称(在括号内,因此它被存储为第 1 组),然后是一些可选的空格字符、冒号、更多可选的空格字符以及后面的任何非空引用值。 所有这些都被引用的项目名称(来自第 1 组!)替换,后跟一个冒号和一个空的引用值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM