[英]Regex for splitting a phone number
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). 国家代码由1-3位数字组成,本地区号由1-3位数字组成,数字长度为4-10位(总共12位数字)。
The separator between the 3 parts can be either a '-'(hyphen) or a ' '(space). 3个部分之间的分隔符可以是' - '(连字符)或''(空格)。
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. 我取了捕获组1,2和3的结果。
But this regex doesn't match any input test case. 但是这个正则表达式与任何输入测试用例都不匹配。 I'm using the java.util.regex package if that helps.
我正在使用java.util.regex包,如果这有帮助的话。 Please suggest a better regex for the same.
请建议一个更好的正则表达式相同。 More info about the problem on https://www.hackerrank.com/challenges/split-number
有关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". 它给了我一个IllegalStateException,意思是“找不到匹配”。
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
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: 您需要使用
Matcher
对象上的find()
方法捕获while循环中的组:
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));
}
}
以下表达式将解析电话号码,从右到左拉各个部分(如果存在),忽略非数字分隔符。
.*?(?<country>[\d]*?)\D*(?<areacode>\d{0,3})\D*(?<prefix>\d{0,3})\D*(?<line>\d{0,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();
}
}
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++;
}
}
} }
It might be easier to use the String.split function, in this example: 在这个例子中,使用String.split函数可能更容易:
String fullNumber = "1-425-9854706";
String[] splitNumber = fullNumber.split("-");
String countryCode = splitNumber[0]; // 1
String areaCode = splitNumber[1]; // 425
String number = splitNumber[2]; // 9854706
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.