简体   繁体   English

用正则表达式打印 Java 中的电话号码,第二个字符和最后四个字符

[英]Masking Phone Number in Java with second character and last four characters printed with regular expressions

I am trying to use regular expression to print the second character and the last four characters of a string.我正在尝试使用正则表达式来打印字符串的第二个字符和最后四个字符。

This is the regular expression I have and its only printing the + and the last four of the string:这是我拥有的正则表达式,它只打印 + 和字符串的最后四个:

s.replaceAll("\\w(?=\\w{4})", "*");

I am using: "+13334445678 " as test dummie data.我正在使用:“+13334445678”作为测试虚拟数据。

I am wanting it to be: +1******5678 I am getting: +*******5678我希望它是:+1******5678 我得到:+*******5678

What am I doing wrong?我究竟做错了什么?

Masking phone number using single regexp may be complicated.使用单个正则表达式屏蔽电话号码可能很复杂。 After some time, it may take a while to recall what regexp (?<?^\+)[0-9](?=[0-9]{4}) does一段时间后,可能需要一段时间才能回忆起正则表达式(?<?^\+)[0-9](?=[0-9]{4})的作用

More straightforward solution is to use substring method and simple replacement replaceAll("\\d", "*") :更直接的解决方案是使用substring方法和简单的替换replaceAll("\\d", "*")

private static String maskPhoneNumber(String tel) {
  if (tel.length() < 6) {
    return tel;
  }
  return tel.substring(0, 2)
      + tel.substring(2, tel.length() - 4).replaceAll("\\d", "*")
      + tel.substring(tel.length() - 4);
}

Examples例子

maskPhoneNumber("+13334445678"); // +1******5678
maskPhoneNumber("+1-333-444-5678"); // +1-***-***-5678
maskPhoneNumber("+1 (333) 444-5678"); // +1 (***) ***-5678

If you don't want to preserve a phone number format (dashes, spaces, brackets), use add one more replacement ( replaceAll("[^\\d]", "") ):如果您不想保留电话号码格式(破折号、空格、括号),请使用添加一个替换( replaceAll("[^\\d]", "") ):

tel.substring(0, 2)
  + tel.substring(2, tel.length() - 4)
      .replaceAll("[^\\d]", "")
      .replaceAll("\\d", "*")
  + tel.substring(tel.length() - 4);

Examples例子

maskPhoneNumber("+13334445678"); // +1******5678
maskPhoneNumber("+1-333-444-5678"); // +1******5678
maskPhoneNumber("+1 (333) 444-5678"); // +1******5678

Such imperative style code is not so elegant as a single regexp but potentially is easier to understand by any developer.这种命令式代码不像单个正则表达式那么优雅,但可能更容易被任何开发人员理解。

If you want to match last 4 digits you can use (.{4})\\s*$ .如果要匹配最后 4 位数字,可以使用(.{4})\\s*$ This regex will return last 4 characters from input string irrespective of white spaces at the end.此正则表达式将从输入字符串返回最后 4 个字符,而不考虑末尾的空格。

Your pattern \w(?=\w{4}) will also match the digit 1 after the plus sign because it matches a word character and asserts that what is on the right should be 4 word characters.您的模式\w(?=\w{4})也将匹配加号后的数字 1,因为它匹配一个单词字符并断言右边的内容应该是 4 个单词字符。

One option is to add a negative lookbehind (?<!^\+) to assert what is on the left is not the start of the string ^ followed by a plus sign.一种选择是在后面添加一个否定的lookbehind (?<!^\+)来断言左边的不是字符串^的开头,后跟一个加号。

Instead of matching a word character, you could match digits [0-9] instead.您可以匹配数字[0-9]而不是匹配单词字符。

(?<!^\+)[0-9](?=[0-9]{4})

Regex demo |正则表达式演示| Java demo Java演示

For example例如

String s = "+13334445678";
System.out.println(s.replaceAll("(?<!^\\+)[0-9](?=[0-9]{4})", "*"));

Output Output

+1******5678

If is is not at the start of the string, another option could be to assert what is on the left is not a plus sign that has no non whitespace char before:如果 is 不在字符串的开头,另一种选择可能是断言左边的不是前面没有非空白字符的加号:

(?<!(?<!\S)\+)[0-9](?=[0-9]{4})

Regex demo正则表达式演示

In case you wanted to preserve any dashes in the format, here is an alternative, using the lambda replace method:如果您想保留格式中的任何破折号,这里有一个替代方法,使用 lambda 替换方法:

public static void main(String[] args) {
    Pattern phoneObfuscator = Pattern.compile("(?<=\\+\\d)[\\d-]+(?=\\d{4})");
    Pattern digit = Pattern.compile("\\d");

    List<String> numbers = List.of("+13334445678", "+1-123-456-7890");
    for (String number : numbers) { 
        String result = phoneObfuscator.matcher(number).replaceAll(
            m -> digit.matcher(m.group()).replaceAll("*"));
        System.out.printf("%s -> %s%n", number, result);
    }
}

It matches the whole string between the +N and NNNN and replaces each digit with * .它匹配 +N 和 NNNN 之间的整个字符串,并用*替换每个数字。

If running a regex multiple times it is usually better to use the compiled version.如果多次运行正则表达式,通常最好使用编译版本。

You can use capture groups to perform this task.您可以使用捕获组来执行此任务。 The regex would match the entire phone number, including the country code.正则表达式将匹配整个电话号码,包括国家代码。 Since you want the masked value to have the country code and last four digits, you'd use capture groups to capture them.由于您希望掩码值具有国家代码和最后四位数字,因此您将使用捕获组来捕获它们。 Then, you can include them in the replacement.然后,您可以将它们包含在替换中。 The below link explains in detail what each part of the regex does.下面的链接详细解释了正则表达式的每个部分的作用。

https://regexr.com/4rcq7 https://regexr.com/4rcq7

String text = "+13334445678 +11234567890";

String regex = "(\\+1)\\d{6}(\\d{4})";
String replacement = "$1******$2";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);

System.out.println(matcher.replaceAll(replacement));

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

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