Java Regex - 屏蔽信用卡号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24238240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:01:07  来源:igfitidea点击:

Java Regex - mask credit card number

javaregexstring

提问by danny.lesnik

I have the function which masks credit card numbers in String:

我有在字符串中屏蔽信用卡号的函数:

  public static String  replaceCreditCardNumber(String text){
         final String MASKCARD = "<MASKED>";
         final Pattern PATTERNCARD = 
            Pattern.compile("([0-9]{4})[0-9]{0,9}([0-9]{4})");    
         Matcher matcher = PATTERNCARD.matcher(text);
         if (matcher.find()) {
             return   matcher.replaceAll(MASKCARD);
         }
         return text;

    }

this function works fine in the following cases:

此功能在以下情况下可以正常工作:

    String text = "Aaaa bbbb aaa=1234567890123456 fdfdfd=aaa";
    String expected = "Aaaa bbbb aaa=1234<MASKED>3456 fdfdfd=aaa";
    assertEquals(expected,text);//OK

    String text = "Aaaa bbbb aaa=\"1234567890123456\" fdfdfd=aaa";
    String expected = "Aaaa bbbb aaa=\"1234<MASKED>3456\" fdfdfd=aaa";
    assertEquals(expected,text);

However the following case fails

但是以下情况失败

        String text = "Aaaa bbbb aaa=1gfg23fgfg4567890123456 fdfdfd=aaa";
        String expected = "Aaaa bbbb aaa=1gfg23fgfg4567890123456 fdfdfd=aaa";
        assertEquals(expected,text);

I'm getting

我越来越

        aaa=1gfg23fgfg4567[<MASKED>]3456

What am I missing in my regex expression?

我的正则表达式中缺少什么?

采纳答案by anubhava

You should use word boundaries to make sure to avoid matching unwanted input:

您应该使用单词边界来确保避免匹配不需要的输入:

final Pattern PATTERNCARD = 
        Pattern.compile("\b([0-9]{4})[0-9]{0,9}([0-9]{4})\b"); 

Working Demo

工作演示

回答by laune

Based on examples:

基于示例:

Pattern.compile("([0-9]{4})[0-9]{8}([0-9]{4})");

回答by Juanes30

I hope to serve you

我希望为您服务

I have the following class:

我有以下课程:

    public class PatternCreditCard {

  // ------------------------- PATTERN DEFINE TYPE CREDIT --------------------------- //
  public static final String VISA = "^4.*";
  public static final String VISA_ELECTRON = "^(4026|417500|4508|4844|491(3|7)).*";
  public static final String MASTER_CARD = "^5[1-5].*";
  public static final String AMEX = "^3[47].*";
  public static final String DISCOVER =
      "^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65).*";
  public static final String DINERS = "^36.*";
  public static final String DINERS_CARTE_BLANCHE = "^30[0-5].*";
  public static final String JCB = "^35(2[89]|[3-8][0-9]).*";

  // ------------------------- PATTERN SEPARATOR GROUP NUMBERS --------------------------- //
  public static final String VISA_SEPARATOR =
      "^([0-9 ]{5})([0-9 ]{5})?([0-9 ]{5})?(?:([0-9 ]{5})([0-9 ]{5})?([0-9]{5})([0-9]{4}))?$";

  public static final String MASTER_CARD_SEPARATOR =
      "^([0-9 ]{5})([0-9 ]{5})?([0-9 ]{5})?(?:([0-9 ]{5})([0-9 ]{5})?([0-9]{5})([0-9]{4}))?$";

  public static final String AMEX_SEPARATOR =
      "^([0-9 ]{5})([ ]?[0-9 ]{7})?(?:([ ]?[0-9 ]{7})([ ]?[0-9]{5}))?$";

  public static final String DISCOVER_SEPARATOR =
      "^([0-9 ]{5})([0-9 ]{5})?([0-9 ]{5})?(?:([0-9 ]{5})([0-9 ]{5})?([0-9]{5})([0-9]{4}))?$";

  // ------------------------- LENGTH PERMITTED FOR CREDIT CARD --------------------------- //
  public static final int VISA_LEN = 16;
  public static final int MASTER_CARD_LEN = 16;
  public static final int AMEX_LEN = 15;
  public static final int DISCOVER_LEN = 16;

}

then do a replacement to the String of the credit card number if the return value is "" (empty): means that the String complies with any group in the regular expression.

如果返回值为“”(空),则对信用卡号的String进行替换:表示该String符合正则表达式中的任意组。

cardNumber.replaceAll(VISA_SEPARATOR, " ");