title: Java Regex - mask credit card number
body: <p>I have the function which masks credit card numbers in String:</p>
  
  <pre><code>  public static String  replaceCreditCardNumber(String text){
           final String MASKCARD = "$1&lt;MASKED&gt;$2";
           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;
  
      }
  </code></pre>
  
  <p>this function works fine in the following cases:</p>
  
  <pre><code>    String text = "Aaaa bbbb aaa=1234567890123456 fdfdfd=aaa";
      String expected = "Aaaa bbbb aaa=1234&lt;MASKED&gt;3456 fdfdfd=aaa";
      assertEquals(expected,text);//OK
  
      String text = "Aaaa bbbb aaa=\"1234567890123456\" fdfdfd=aaa";
      String expected = "Aaaa bbbb aaa=\"1234&lt;MASKED&gt;3456\" fdfdfd=aaa";
      assertEquals(expected,text);
  </code></pre>
  
  <p>However the following case fails</p>
  
  <pre><code>        String text = "Aaaa bbbb aaa=1gfg23fgfg4567890123456 fdfdfd=aaa";
          String expected = "Aaaa bbbb aaa=1gfg23fgfg4567890123456 fdfdfd=aaa";
          assertEquals(expected,text);
  </code></pre>
  
  <p>I'm getting </p>
  
  <pre><code>        aaa=1gfg23fgfg4567[&lt;MASKED&gt;]3456
  </code></pre>
  
  <p>What am I missing in my regex expression?</p>