libphonenumber requires a phone number and a country name as a parameter
to verify a phone number.
PhoneNumber numberProto = phoneUtil.parse("044 668 18 00", "CH");
System.out.println(numberProto);
System.out.println("is valid: "+phoneUtil.isValidNumber(numberProto));
I can find phone numbers from a text document using regex, but not country name associated with that particular phone number.
Please, suggest me a way to find valid phone numbers from text.
Example input:
..some text.. first number +13478093374 ..some text..
some new text.. second number.. +91 774-5001-827 ..some text.
some new text.. third number.. 044 668 18 00 ..some text.
some new text.. forth number.. 020-2689-0455 ..some text.
so the respective output should be,
phoneUtil.parse("044 668 18 00", "CH") //valid
phoneUtil.parse("+91 774-5001-827", "IN") //valid
phoneUtil.parse("+13478093374", "US") //valid
phoneUtil.parse("020-2689-0455", "IN") //valid
Please suggest a algorithm to add country parameter.
You can use the regex like this:
"(\d\d\d)-(\d\d\d\d)-(\d\d\d)\s"
Then use the code like this:
Pattern pattern = Pattern.compile("(\\d\\d\\d)-(\\d\\d\\d\\d)-(\\d\\d\\d)\\s");
String phoneNumber = "";
String text = "..some text.. valid number 774-0000-827 ..some text..";
Matcher matcher = pattern.matcher(text);
if (matcher.find())
{
phoneNumber += matcher.group(1) + " ";
phoneNumber += matcher.group(2) + " ";
phoneNumber += matcher.group(3) + " ";
PhoneNumber numberProto = phoneUtil.parse(phoneNumber, "CH");
System.out.println(numberProto);
System.out.println("is valid: "+ phoneUtil.isValidNumber(numberProto));
}
Related
I am trying to read otp number from given String i have applied this below string but i am getting two number can any one please help me how to get otp number
String str="Your OTP for Darshann is : 9999%n 12341234123";
String numberOnly= str.replaceAll("[^0-9]", "");
I want to read number just after Your OTP for Darshann is : this text which is 9999 i
By replacing with empty string "" you are concatenating the numbers. This is why have incorrect results.
Try this instead:
String str="Your OTP for Darshann is : 9999%n 12341234123";
String numberOnly= str.replaceAll("[^0-9]", " ");
List<String> numbers = Arrays.asList(numberOnly.trim().split(" ")).stream()
.filter(s->!s.isBlank())
.collect(Collectors.toList());
System.out.println(numbers);
This would give a list of all numbers found in the text:
[9999, 12341234123]
Of course if there is more than one number in the string this function will produce more than one result.
String message = "Your OTP for Darshann is 1234";
// split the message by "is"
String[] parts = message.split("is ");
String OTP = parts[1];
// rgx
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(OTP);
if (matcher.find()) {
String OTPnumber = matcher.group();
System.out.print("OTP is: " + OTPnumber);
} else {
System.out.println("not found");
}
I am using libphonenumber to extract phone number from a given String but it has failed to extract numbers with "+34 (0)" prefix (+34 prefix works fine). It works fine with other extensions (ie :- +38 (0) ). It looks like it is linked to +34 combined with (0) pattern. Following is the code sample. Anyone experienced this before?
String content = someString + "+34 (0)xxx - xxxxxx" + someString;
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Iterable<PhoneNumberMatch> intlNumbers = phoneNumberUtil.findNumbers(content, null);
You can use parse method then get nationalNumber and countryCode
public static void main(String[] args) throws NumberParseException {
String content = "+34 (0)123 - 456789";
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
PhoneNumber phoneNumber =phoneNumberUtil.parse(content, null);
System.out.println("Phone Number = "+phoneNumber.getNationalNumber());
System.out.println("Country Code = "+phoneNumber.getCountryCode());
}
Result
Phone Number = 123456789
Country Code = 34
Telephone numbers in Spain shows that 0 should have been 6 or 7. I think as not living in Spain.
I am wanting to create a regex for the following number formats:
+XXXXXXXXXX. +1(XXX)xxxxxx, +x(xxx)-xxx-xxxx, xxx-xxx-xxxx, xxx-xxxx, and Phone Number:,Phone:,Tel: with all the above formats. All with the output of xxxxxxxxxx
Below is a snippet of my code.
public static String getPhoneNumber() // returns the phone number formatted as a sequence of digits
{
String regex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})(?:Tel:)$";
Pattern pattern = Pattern.compile(regex);
for (int i = 0; i < line.length(); i++)
{
//if phone number format includes -, . , spaces, + sign in front
if (line.matches("[+]?\\d?[- .]?(\\([0-9]\\d{2}\\)|[0-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$")) {
phoneNumber = line.replaceAll("[^\\d.]", "").replace("-", "").replace(".", "").replace(" ", "").replace("(", "").replace(")", "")
.replace("+", "");
}
else
{
getEmailAddress();
}
}
//System.out.println(phoneNumber);
return phoneNumber;
}
Try regex ^(?:(?:Tel|Phone Number|Phone): )?(\+?\(?\d{3}\)?[-. ]\d{3}[-. ]\d{4})$.
This will match the phone numbers with the keywords Phone,Tel or Phone Number and not with others.
Capture group $1 to get the phone number.
Regex
It seems you want to remove all non-digits, so just do that. To select lines, match those that have (at least) 10 digits:
if (line.matches("(\\D*\\d){10}.*"))) {
phoneNumber = line.replaceAll("\\D", "");
}
is all you need.
String pattern = "\d{10}|(?:\d{3}-){2}\d{4}|\(\d{3}\)\d{3}-?\d{4}";
Here is my string
INPUT:
22 TIRES (2 defs)
1 AP(PEAR + ANC)E (CAN anag)
6 CHIC ("SHEIK" hom)
EXPECTED OUTPUT:
22 TIRES
1 APPEARANCE
6 CHIC
ACTUAL OUTPUT :
TIRES
APPEARANCE
CHIC
I tried using below code and got the above output.
String firstnames =a.split(" \\(.*")[0].replace("(", "").replace(")", "").replace(" + ",
"");
Any idea of how to extract along with the numbers ? I don't want the numbers which are after the parentheses like in the input " 22 TIRES (2 defs)". I need the output as "22 TIRES" Any help would be great !!
I am doing it bit differently
String line = "22 TIRES (2 defs)\n\n1 AP(PEAR + ANC)E (CAN anag)\n\n6 CHIC (\"SHEIK\" hom)";
String pattern = "(\\d+\\s+)(.*)\\(";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
String tmp = m.group(1) + m.group(2).replaceAll("[^\\w]", "");
System.out.println(tmp);
}
Ideone Demo
I would use a single replaceAll function.
str.replaceAll("\\s+\\(.*|\\s*\\+\\s*|[()]", "");
DEMO
\\s+\\(.*, this matches a space and and the following ( characters plus all the remaining characters which follows this pattern. So (CAN anag) part in your example got matched.
\\s*\\+\\s* matches + along with the preceding and following spaces.
[()] matches opening or closing brackets.
Atlast all the matched chars are replaced by empty string.
I have following regular expression for following mobile numbers:
^(([+]|[0]{2})([\\d]{1,3})([\\s-]{0,1}))?([\\d]{10})$
Valid numbers are:
+123-9854875847
00123 9854875847
+123 9854875847
9878757845
Above expression will not validate if user enter 9 or 11 digit mobile number but if user enter 9 or 11 digit number with +123 or +91 respectively then it is getting validate because in this part of expression ([\\d]{1,3}) last two digits are optional.
So, any way to make this part ([\\s-]{0,1}))?([\\d]{10}) not to get combine with this part ([\\d]{1,3})?
The question is somewhat unclear, but I presume you want to split the number and the country code.
This is quite easy to do by extracting groups. group(i) is the i-th thing in brackets.
I also applied these simplifications: [\\d] = \\d, {0,1} = ?, [+] = \\+, [0]{2} = 00.
Code:
String regex = "^((\\+|00)(\\d{1,3})[\\s-]?)?(\\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
System.out.println("Country = " + m.group(3));
System.out.println("Data = " + m.group(4));
}
Output:
Country = 123
Data = 9854875847
Alternative using non-matching groups (?:): (so you can use group(1) and group(2))
String regex = "^(?:(?:\\+|00)(\\d{1,3})[\\s-]?)?(\\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
System.out.println("Country = " + m.group(1));
System.out.println("Data = " + m.group(2));
}
Reference.
Related test.
As long as the extension is always separated from the rest of the phone number, your regex will work fine. If there is no such separation, there is no way to correctly validate a phone number.
Also keep in mind that both extensions and phone numbers can vary in length from country to country, so there is no regex that will solve all cases. If you can produce a list of allowed extensions, you can work that into the regex and get better matches, but for many groups of arbitrary length of digits you will get many wrong matches.
I have simplified your regex a bit, so oyu can see #Dukeling's suggestions in practice. Your regex on top, mine on the bottom.
^(([+]|[0]{2})([\\d]{1,3})([\\s-]{0,1}))?([\\d]{10})$
^( (\\+|00) \\d{1,3} [\\s-]?)? \\d{10} $
try {
String mobile_number="india number +919979045000\n" +
"india number 9979045000\n" +
"china number +86 591 2123654\n" +
"Brazil number +55 79 2012345\n" +
"it is test all string get mobile number all country"+
"Ezipt +20 10 1234567\n" +
"France +33 123456789\n" +
"Hong Kong +852 1234 5456\n" +
"Mexico +52 55 12345678"+
"thanks";
Pattern p = Pattern.compile("\\(?\\+[0-9]{1,3}\\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{5}( ?-?[0-9]{3})? ?(\\w{1,10}\\s?\\d{1,6})?");
List<String> numbers = new ArrayList<String>();
//mobile_number= mobile_number.replaceAll("\\-", "");
Matcher m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
p = Pattern.compile("\\(?\\+[0-9]{1,3}\\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?(\\w{1,10}\\s?\\d{1,6})?");
m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
p = Pattern.compile("((?:|\\+)([0-9]{5})(?: |\\-)(0\\d|\\([0-9]{5}\\)|[1-9]{0,5}))");
m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
p = Pattern.compile("[0-9]{10}|\\(?\\+[0-9]{1,3}\\)?-?[0-9]{3,5} ?-?[0-9]{4}?");
m = p.matcher("" + mobile_number);
while (m.find()) {
numbers.add(m.group());
}
String numberArray=numbers.toString();
System.out.print(""+numberArray);
// final result
/* [+919979045000, +86 591 2123654, +33 123456789, +52 55 12345678, +919979045000, +86 591 2123654, +55 79 2012345, +20 10 1234567, +33 123456789, +852 1234 5456, +52 55 12345678, +919979045000, 9979045000] */
} catch (Exception e) {
e.printStackTrace();
}
Best way to take input in two parts i.e country code and mobile number.
In that case you can easily validate it (both country code and mobile number) with regex.