英文:
Regex JSON String Mask
问题
你可以使用以下正则表达式来处理这个情况:
String s = "{ \"Name1\":\"Jo\", \"pass\":\"123456789123\" }";
System.out.println(s
.replaceAll("(?<=Name1\":\")(.{1,5})(.*?)", "*****$2")
.replaceAll("(?<=pass\":\")(.{1,5})(.*?)", "*****$2")
);
这将确保即使 "Name1" 的长度少于 5 个字符,也会在其后添加逗号。
英文:
I am using the following code to mask first 5 chars of JSON Value and it works as expected.
String s = "{ \"Name1\":\"Cristhoper David\", \"pass\":\"123456789123\" }";
System.out.println(s
.replaceAll("(?<=Name1\":\")(.{1,5})(.*?)", "*****$2")
.replaceAll("(?<=pass\":\")(.{1,5})(.*?)", "*****$2")
);
Output:
{ "Name1":"*****hoper David", "pass":"*****6789123" }
But when the Name1 is less than 5 chars the JSON is getting improper and "
and ,
signs are getting disappeared as in following example:
String s = "{ \"Name1\":\"Joe\", \"pass\":\"123456789123\" }";
System.out.println(s
.replaceAll("(?<=Name1\":\")(.{1,5})(.*?)", "*****$2")
.replaceAll("(?<=pass\":\")(.{1,5})(.*?)", "*****$2")
);
Output:
{ "Name1":"***** "pass":"*****6789123" }
Here "
and ,
got removed.
And for the following as well
String s = "{ \"Name1\":\"Jo\", \"pass\":\"123456789123\" }";
System.out.println(s
.replaceAll("(?<=Name1\":\")(.{1,5})(.*?)", "*****$2")
.replaceAll("(?<=pass\":\")(.{1,5})(.*?)", "*****$2")
);
Output:
{ "Name1":"*****"pass":"*****6789123" }
If Name1
is less than 5 chars I want it to be
{ "Name1":"*****","pass":"*****6789123" }
How should I correct my regex to handle this?
答案1
得分: 1
初始问题:
将值的前5个字符替换为掩码,并且如果值少于5个字符,仍然用5个字符的常量替换。OP希望在以后的阶段为Json和XML执行此操作。
实际问题:
通过评论变得清楚,OP实际上希望替换XML字符串中值的前5个字符。因此,请尝试:
(?<=<(?:Name1|pass)>[^<]{0,4})[^<]
在线演示请查看这里。
(?<=
- 打开正向回顾后查找;(?:Name1|pass)
- 非捕获组,匹配文字'Name1'或'pass';>[^<]{0,4})
- 匹配文字'>'和在关闭回顾前的0-4个非'<'字符;
[^<]
- 匹配单个非'<'字符。
相同的技术可以应用于Json字符串:
(?<="(?:Name1|pass)":"[^"]{0,4})[^"]
或者,稍微宽松一点,但现在可能已足够的选项混合:
(?<=[<"](?:Name1|pass)(>|":"[^<"]{0,4})[^<"]
英文:
Initial question:
Mask first 5 characters of value and if value is less than 5 characters, still replace with a constant of 5 characters. OP wanted this done for Json and XML in a later stage.
Actual question:
It became clear through the comments that OP is actually looking to replace up to the first 5 characters of a value within XML-string. Therefor try:
(?<=<(?:Name1|pass)>[^<]{0,4})[^<]
See an online demo
(?<=
- Open positive lookbehind;(?:Name1|pass)
- Non-capture group to match either 'Name1' or 'pass' literally;>[^<]{0,4})
- Match a literal '>' and 0-4 characters other than '<' before we close the lookbehind;
[^<]
- Match a single character other than '<'.
The same technique could be applied for a Json-string:
(?<=\"(?:Name1|pass)\":\"[^\"]{0,4})[^\"]
Or, a little less strict, but probably good enough for now, a mix of both options:
(?<=[<\"](?:Name1|pass)(?:>|\":\")[^<\"]{0,4})[^<\"]
答案2
得分: 0
被添加的模式 |(?<=Name1\":\)(.+?)"
匹配了 Name1 字段少于 5 个字符的情况。 .+?
表示一个或多个字符。
英文:
The added pattern |(?<=Name1\":\)(.+?)"
matches the case where Name1 has fewer than 5 characters. The .+?
means one or more characters.
答案3
得分: 0
你可以通过将你的正则表达式更改为以下方式来解决这个问题:
(?<=Name1\"):([^"]{1,5})(.*?)
.
匹配任何字符[^"]
匹配除了"
之外的所有字符。
因此它会停在名称后面的"
。
所选组 ($1
) 可以包含 1 到 5 个字符。
如果Name1
是""
,那么输出中也将是""
。
顺便说一下:你在 "pass" 上也有相同的问题。
你的代码应该如下所示:
System.out.println(s.
replaceAll("(?<=Name1\"):([^\"]{1,5})(.*?)", "*****$2").
replaceAll("(?<=pass\"):([^\"]{1,5})(.*?)", "*****$2")
);
英文:
You can solve this if you change your regex like this
(?<=Name1\":\")([^"]{1,5})(.*?)
.
matches any character[^"]
matches all but"
.
So it will stop at the"
after the name.
The selected group ($1
) then can have 1..5 characters.
IfName1
is""
, then it will be""
in the output too.
By the way: you have the same problem with "pass".
Your code shoud look like:
System.out.println(s.
replaceAll("(?<=Name1\":\")([^\"]{1,5})(.*?)", "*****$2").
replaceAll("(?<=pass\":\")([^\"]{1,5})(.*?)", "*****$2")
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论