我想写一个RegEx来涵盖以下电话号码:
+91 33 1234 5678 (landline with two digit city code. 33 in this example)
+91 123 1234 5678 (landline with three digit city code. 123 in this example)
+91 12345 67890 (mobile no. Mobile no starts with 9 or 8 or 7)
任何有关解释的帮助都会非常有帮助。
我正在使用的代码是
\+91\s([\d]{2,3}\s)?[\d]{2,5}\s[\d]{3,4}
答案 0 :(得分:0)
好吧,首先你需要先改变你的正则表达式。最后一个数字有5位数。因此,您必须更改4
的{{1}}。然后,删除括号。你不需要它们。然后,正则表达式适用于任何类型的语言。您需要使用双5
,因为\\
是转义字符。
\
打印:
public static void main(String[] args)
{
String line = "+91 33 1234 5678 (landline with two digit city code. 33 in this example) +91 123 1234 5678 (landline with three digit city code. 123 in this example) +91 12345 67890 (mobile no. Mobile no starts with 9 or 8 or 7)";
String pattern = "\\+91\\s(\\d{2,3}\\s)?\\d{2,5}\\s\\d{3,5}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println("Found value: " + m.group(0));
}
}