title: how to remove unmatched data while comparing with regex using java
body: <p>hope doing good.. I am trying to remove unmatched data while comparing with regex using java. And My requirement is like this. 
  My Regular Expression is:</p>
  
  <pre><code>([A-Z]{5}[0-9]{4}[A-Z])(XM|XD|EM|ED)([0-9]{3})(_PRA_)([0-9]{3})
  </code></pre>
  
  <p>It should accept string like this:</p>
  
  <pre><code>SHIVA1234AXM123_PRA_123
  </code></pre>
  
  <p>But in case of this if I give any thing <code>aaSHIVA1234AXM123_PRA_123aa</code> (or) <code>SHIVA1234AXccccM123_PRA_123</code> like this method should give <code>aaaa</code> (or) <code>cccc</code> like this.
  I have tried the following code:</p>
  
  <pre><code>public class ExpressionDemo {
      public static void main(String[] args) {
          String regex = "([A-Z]{5}[0-9]{4}[A-Z])(XM|XD|EM|ED)([0-9]{3})(_PRA_)([0-9]{3})";
          Pattern p = Pattern.compile(regex);
  
          String str="SHIVA1234AXM123_PRA_123";
          Matcher m = p.matcher(str);  
          System.out.println(m.groupCount());
  
          for(int i=1;i&lt;=m.groupCount();i++)
          {
              int start=0;
              int end=0;
              System.out.println("count "+i);
              if(m.lookingAt())
              {
                  System.out.println(m.group(i));
                  System.out.println("start of group"+m.start(i));
                  System.out.println("end of group"+m.end(i));
                  start=m.start(i);
                  end=m.end(i);
                  m.reset();
              }
              else
              {
                  System.out.println("ELSE");
                  System.out.println(start);
                  System.out.println(end);
              }
          }
      }
  }
  </code></pre>
  
  <p>This is for positive case but i need this for false case. And it should be generic for any expression--->string.</p>