extract numbers from string – java

for example if you want to compare any phone number with actual value, 555-666-777 and want to avoid hyphens or what ever..and extract only numbers use replaceall with correct regular expressions. simply do replace the non digits with blank value.

String phoneNumber  = 555-666-777

example : assertEquals(phoneNumber.replaceAll(“\\D+”,””), driver.findElement(By.id(“storePhoneNo”)).getText().replaceAll(“\\D+”,””));

result will be 555666777

No images available.