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.
Related
Let's say I have this String:
String phoneNumber = "+15611234567"
Using PhoneNumberUtils.formatNumber(phoneNumber); works here and formats it into +1-561-123-4567
However, when I have a non-US phone number with its country code included, such as
String phoneNumber = "+96170123456" //Lebanese phone number
It doesn't work, it just returns it as-is instead of it being formatted.
Returns: +96170123456
Expected: +961 81-932-452 or any kind of different formatting (e.g +961 70 123 456)
In case you need it, here's the code that's not working:
public void setPhone(final String phone){
TextView phoneTV = view.findViewById(R.id.phone);
String formattedNumber = PhoneNumberUtils.formatNumber(phone);
phoneTV.setText(formattedNumber);
}
The phone number's country code isn't consistent, it could be from any country.
formatNumber (String phoneNo) This method was deprecated in API level 21.
So for API level 21 or above use :
formatNumber(String phoneNumber, String defaultCountryIso)
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}";
I want to get value from web
For eg : Total AMount : 25000
and want to convert this value into integer to proceed comparision step but after printing totat_Amt it displayed as
"java.lang.NumberFormatException: For input string: "25,000""
Here is my code :
WebElement gt = driver.findElement(By.id("totAmt"));
String total_Amt=gt.toString();
System.out.println("Total Amt:"+total_Amt);
//int total_amt_val =Integer.parseInt(total_Amt);
System.out.println(total_amt_val);
Try to replace all non-numeric symbols:
WebElement gt = driver.findElement(By.id("totAmt"));
String total_Amt = gt.toString(); // "25,000"
// replace 'bad' symbols
String onlyNumbers = total_Amt.replaceAll("[^\\d]", ""); // "25000"
System.out.println("Total Amt: " + total_Amt);
int total_amt_val = Integer.parseInt(onlyNumbers); // 25000
System.out.println(total_amt_val);
\\d means all numbers, [^\\d] means all non-numbers, and if you want to keep some another symbols, just add them into [] — e.g. use [\\d.] if you want to keep dots too.
You can use NumberFormat
NumberFormat.getNumberInstance(Locale.US).parse("25,000").intValue() will return 25000
You can try this:
WebElement gt = driver.findElement(By.id("totAmt"));
String total_Amt = gt.toString(); // 25,000
total_Amt = total_Amt.replaceAll(",", ""); // removes all ',' -> 25000
int total_amt_val = Integer.parseInt(total_Amt); // 25000 as int already
System.out.println(total_amt_val); // 25000
You can replace all the comma first using replaceAll method and then directly parse it as below. additionally, you need to use getText() method should be used to retrieve the element text.
WebElement gt = driver.findElement(By.id("totAmt"));
//To be changed as gt.getText().
String total_Amt=gt.getText();
System.out.println("Total Amt:"+total_Amt);
//Replace comma as empty and then you can normal parse the string to int
int total_amt_val =Integer.parseInt(total_Amt.replaceAll(",",""));
System.out.println(total_amt_val)
In case , If you are getting the total_Amt value as Total AMount : 25000, then extract the amount value using substring method and then replace all the , as empty using replaceAll method
int total_amt_val =Integer.parseInt(total_Amt.substring(total_Amt.indexOf(":")+2).replaceAll(",",""));
If you are dealing with price, use this:
public static void main(String[] args) throws Exception {
WebElement gt = driver.findElement(By.id("totAmt"));
String total_Amt = gt.getText(); // total_Amt=25,000.00
BigDecimal bd_amt = parse(total_Amt , Locale.US); // Use this if the value is price
int int_amount = parse(total_Amt , Locale.US).intValueExact(); // Use this if you want integer
System.out.println("Price : " + bd_amt);
System.out.println("Amount : " + int_amount);
}
private static BigDecimal parse(final String amount, final Locale locale) throws ParseException {
final NumberFormat format = NumberFormat.getNumberInstance(locale);
if (format instanceof DecimalFormat) {
((DecimalFormat) format).setParseBigDecimal(true);
}
return (BigDecimal) format.parse(amount.replaceAll("[^\\d.,]", ""));
}
Sample Output:
Price : 25000.00
Amount : 25000
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));
}
I need to split a phone number (can be mobile or land line) into:
country dialing code
area code
phone number
Examples:
+923211234567 will be 92 32x 1234567 (mobile)
+92992123456 will be 92 992 123456 (land line)
Let's say I have a database with all countries' dialing code, area code, mobile country code, mobile network code...
The problem is that Country Dialing Code can be 1, 2 or 3 digits. For example:
1 for USA, 92 for Pakistan, or 233 for Ghana
Also, Area and Mobile Network Code can be 2 or 3 digits. Any ideas?
You can use the Phone number parser library.
A demo page can be found here.
Is that known before splitting whether the number is a Mobile number or a Land line number? If not then even the number can vary in length.
try {
// phone must begin with '+'
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber numberProto = phoneUtil.parse("your number", "");
int countryCode = numberProto.getCountryCode();
long nationalNumber = numberProto.getNationalNumber();
Log.i("code", "code " + countryCode);
Log.i("code", "national number " + nationalNumber);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}