title: Capturing groups and Pattern split method in regular expression
body: <p>How can I understand the output of the below code? The code's first four print statements are about the Capturing Groups in Regular Expression in Java and the rest of the code is about the <code>Pattern</code> <code>split</code> method. I referred a few documents to perceive the code's output (shown in the pic) but could not figured it out how exactly it's working and showing this output.</p>
  
  <p><strong>Java Code</strong></p>
  
  <pre><code>    import java.util.*;
      import java.util.regex.*;
      import java.lang.*;
      import java.io.*;
  
      /* Name of the class has to be "Main" only if the class is public. */
      public class Codechef
      {
          public static void main(String[] args) {
              //Capturing Group in Regular Expression
              System.out.println(Pattern.matches("(\\w\\d)\\1", "a2a2")); //true
              System.out.println(Pattern.matches("(\\w\\d)\\1", "a2b2")); //false
              System.out.println(Pattern.matches("(AB)(B\\d)\\2\\1", "ABB2B2AB")); //true
              System.out.println(Pattern.matches("(AB)(B\\d)\\2\\1", "ABB2B3AB")); //false
              // using pattern split method
              Pattern pattern = Pattern.compile("\\W");
              String[] words = pattern.split("one@two#three:four$five");
              System.out.println(words);
              for (String s : words) {
                  System.out.println("Split using Pattern.split(): " + s);
              }
  
          }
      }
  </code></pre>
  
  <p><strong>Results</strong></p>
  
  <p><a href="https://i.stack.imgur.com/g25tU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g25tU.png" alt="enter image description here"></a></p>
  
  <p>Edit-1 </p>
  
  <p><strong>Queries</strong></p>
  
  <ul>
  <li>If I talk about Capturing Groups, I cannot figure out what’s use of ‘\1’ or ‘\2’ here? How these are evaluating to true or false.</li>
  <li>If I talk about Pattern split method, I wish to know how the string split is happening. How does this split method work differently than a normal string split method?</li>
  </ul>