title: Regex for splitting a phone number
body: <p>I'm trying to solve a problem as described below:</p>
  
  <p>Given a phone number, split it into country code, local area code and number. The number format is - </p>
  
  <pre><code>[Country code]-[Local Area Code]-[Number]
  </code></pre>
  
  <p>Country code consists of 1-3 digits, local area code consists of 1-3 digits and the number is 4-10 digits long (total 12 digits always).</p>
  
  <p>The separator between the 3 parts can be either a '-'(hyphen) or a ' '(space).</p>
  
  <p>Example:</p>
  
  <pre><code>Given Number = 1-425-9854706
  Result --&gt; Group 1 = 1          --&gt; Country Code
             Group 2 = 425        --&gt; Local Area Code
             Group 3 = 9854706    --&gt; Number
  </code></pre>
  
  <p>The regex i'm using currently for the same is - </p>
  
  <pre><code>^(\\d{1,3})[- ](\\d{1,3})[- ](\\d{4,10})$
  </code></pre>
  
  <p>I take the result of capture group 1, 2 and 3.</p>
  
  <p>But this regex doesn't match any input test case. I'm using the java.util.regex package if that helps. Please suggest a better regex for the same. More info about the problem on <a href="https://www.hackerrank.com/challenges/split-number" rel="nofollow">https://www.hackerrank.com/challenges/split-number</a></p>
  
  <p>The code i have written is this:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  import java.text.*;
  import java.math.*;
  import java.util.regex.*;
  
  public class Solution {
  
      public static void main(String[] args) {
          /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
  
          Scanner sc = new Scanner(System.in);
          int n = Integer.parseInt(sc.nextLine());
  
          ArrayList&lt;ArrayList&lt;Integer&gt;&gt; output = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();
  
          String s;
  
          String REGEX = "^(\\d{1,3})[- ](\\d{1,3})[- ](\\d{4,10})$";
          Pattern pattern = Pattern.compile(REGEX);
  
          Matcher matcher;
  
          for(int i=0 ; i&lt;n ; i++) {
              ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  
              s = sc.nextLine();
              matcher = pattern.matcher(s);
  
              list.add(Integer.parseInt(matcher.group(1)));
              list.add(Integer.parseInt(matcher.group(2)));
              list.add(Integer.parseInt(matcher.group(3)));
  
              output.add(list);
          }
  
          sc.close();
  
          for(int i=0 ; i&lt;n ; i++) {
              System.out.print("CountryCode="+output.get(i).get(0)+",");
              System.out.print("LocalAreaCode="+output.get(i).get(1)+",");
              System.out.print("Number="+output.get(i).get(2));
              System.out.println("");
          }
      }
  }
  </code></pre>
  
  <p>It's giving me a IllegalStateException, meaning "no match found".</p>