Split the Phone Numbers

Sort by

recency

|

115 Discussions

|

  • + 0 comments

    perl

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    while(<>){
       if(/(\d{1,3}?)[-\s](\d{1,3}?)[-\s](\d+)/){
        print "CountryCode=$1,LocalAreaCode=$2,Number=$3\n";
       }
    	}
    
  • + 0 comments

    Java 15

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = Integer.parseInt(scanner.nextLine());
            Pattern pattern = Pattern.compile("^(\\d{1,3})[- ](\\d{1,3})[- ](\\d{4,10})");
            for(int i=0;i<n;i++)
            {   Matcher matcher = pattern.matcher(scanner.nextLine());
                if (matcher.find())
                {   System.out.print("CountryCode="+matcher.group(1));
                    System.out.print(",LocalAreaCode="+matcher.group(2));
                    System.out.println(",Number="+matcher.group(3));
                }
            }
        }
    }
    
  • + 0 comments

    If you are using Java, Use the split("") to split the Input into String[] (An Array). Note: The matched part is excluded in the result and whatever didn't matche the RegEx is returned in the array.

  • + 0 comments

    Something something like this:

    import re
    
    regex = re.compile(r'(\d+)(?:-| )(\d+)(?:-| )(\d+)')
    
    n = int(input())
    for _ in range(n):
        m = regex.findall(input())[0]
        print('CountryCode={},LocalAreaCode={},Number={}'.format(*m))
    
  • + 0 comments
    import re
    
    [print(re.sub(r"(\d{1,3})[\-\s](\d{1,3})[\-\s](\d{4,10})", r"CountryCode=\1,LocalAreaCode=\2,Number=\3", input())) for _ in range(int(input()))]