[英]Java Regex to mask alphanumeric string and show last 4 digits
I have an input string that looks like any of the following:我有一个类似于以下任何一个的输入字符串:
Basically the string can contain alphabet characters or digits.基本上字符串可以包含字母字符或数字。 However it cannot contain symbols, just letters and numbers.但是它不能包含符号,只能包含字母和数字。 I would like to mask it so that it looks like this:我想掩盖它,使其看起来像这样:
I've looked everywhere to find an java code example that uses regex to mask this.我到处寻找使用正则表达式来掩盖它的Java代码示例。 I've found this post on stack but that assumes that the input is purely a number.我在堆栈上找到了这篇文章,但假设输入纯粹是一个数字。
I adjusted the regex to /\\w(?=\\w{4})/g
to include characters as well.我将正则表达式调整为/\\w(?=\\w{4})/g
以包含字符。 It seems to work here .它似乎在这里工作。 But when I try to implement it in java it doesn't work.但是当我尝试在 java 中实现它时,它不起作用。 Here's the line in my java code:这是我的java代码中的一行:
String mask = accountNumber.replace("\\w(?=\\w{4})", "*");
The mask ends up being the same as the accountNumber.掩码最终与 accountNumber 相同。 So obviously the regex is not working.所以显然正则表达式不起作用。 Any thoughts?有什么想法吗?
You're using replace , which doesn't use regular expressions.您正在使用replace ,它不使用正则表达式。
Try replaceAll .尝试replaceAll 。
使用过的replaceAll替换
String mask = accountNumber.replaceAll("\\w(?=\\w{4})", "*");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.