问题
I'm trying to solve a problem as described below:
Given a phone number, split it into country code, local area code and number. The number format is -
[Country code]-[Local Area Code]-[Number]
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).
The separator between the 3 parts can be either a '-'(hyphen) or a ' '(space).
Example:
Given Number = 1-425-9854706
Result --> Group 1 = 1 --> Country Code
Group 2 = 425 --> Local Area Code
Group 3 = 9854706 --> Number
The regex i'm using currently for the same is -
^(\\d{1,3})[- ](\\d{1,3})[- ](\\d{4,10})$
I take the result of capture group 1, 2 and 3.
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 https://www.hackerrank.com/challenges/split-number
The code i have written is this:
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<ArrayList<Integer>> output = new ArrayList<ArrayList<Integer>>();
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<n ; i++) {
ArrayList<Integer> list = new ArrayList<Integer>();
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<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("");
}
}
}
It's giving me a IllegalStateException, meaning "no match found".
回答1:
This works:
package de.lhorn.stackoverflow;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern phoneNumber = Pattern
.compile("^(\\d{1,3})[- ](\\d{1,3})[- ](\\d{4,10})$");
Matcher matcher = phoneNumber.matcher("1-425-9854706");
if (matcher.matches()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
}
}
}
Output:
1
425
9854706
回答2:
Country Code : 1
Local Area Code : 425
Number : 9854706
You need to capture the groups in a while-loop, using the find() method on the Matcher object:
import java.util.regex.*;
public class PhoneRegexTest {
public static void main (String[] args) {
String line = "1-425-9854706";
Pattern pattern = Pattern.compile("^(\\d{1,3})[- ](\\d{1,3})[- ](\\d{4,10})$");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
printMatch("Country Code", matcher, 1);
printMatch("Local Area Code", matcher, 2);
printMatch("Number", matcher, 3);
}
}
public static void printMatch(String label, Matcher m, int group) {
System.out.printf("%-16s: %s%n", label, m.group(group));
}
}
回答3:
The following expressions will parse a phone number, pulling the various parts from right to left, if they exist, ignoring non-digit separators.
.*?(?<country>[\d]*?)\D*(?<areacode>\d{0,3})\D*(?<prefix>\d{0,3})\D*(?<line>\d{0,4})$
回答4:
//for an application that accepts user input
//user input in the form (555) 555-5555
import java.util.Scanner;
public class PhoneNumberTokenizer
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter phone number to tokenize: ");
String phoneNumber = scanner.nextLine();
String[] tokens = phoneNumber.split("\\D+");
System.out.println("The tokens are: ");
for(String token : tokens)
System.out.println(token);
System.out.println();
}
}
回答5:
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int a =sc.nextInt();
sc.nextLine();
int i =1;
while(i<=a)
{
String s =sc.nextLine();
String[] words =s.trim().split("[ -]");
System.out.println("CountryCode="+words[0]+","+"LocalAreaCode="+words[1]+"," +"Number="+words[2]);
i++;
}
}
}
回答6:
It might be easier to use the String.split function, in this example:
String fullNumber = "1-425-9854706";
String[] splitNumber = fullNumber.split("-");
String countryCode = splitNumber[0]; // 1
String areaCode = splitNumber[1]; // 425
String number = splitNumber[2]; // 9854706
来源:https://stackoverflow.com/questions/26253503/regex-for-splitting-a-phone-number