Is there any Java or Javascript library that deals with email/phone numbers detection within text? I'm not talking about validating them, just want to find something that could potentially look like an email address of a phone number.

We're trying to prevent users to send this kind of information through our platform.

1 Answer 1


Just use regular expressions.

For an email address just use the regular expression for at least one letter or number followed by @ followed by at least one letter or number a dot and at least one more letter, so: [email protected] could possibly be an email address as could [email protected] ect. a possible re would be \w+@\w\.\w

Telephone numbers would be, depending on whether you are looking for long distance numbers only, something like \d{2}[ -]*\d{3}[ -]*\d{3,} but you might be better off locating potential phone numbers with a regex then using the google libphonenumber java/JS library which is a much more sophisticated library for checking and validating phone numbers and allows you to check if something is a possible phone number and if it is a valid phone number.

  • 2
    As TLDs do have at least 2 chars, and 2nd level domains usually as well, one could improve the related regex to [w\.]+@(\w{2,}\.)+\w{2,} (to also take into account multilevel domains like abc.xyz.com). Phone numbers are a bit more tricky: from using formatting like +1-123-55555-321 via +1 (0123) 55555 321 to just 012355555321, everything could be a phone number. As one cannot filter all [0-9\+\-]*, this would require a more sophisticated filter – which some library might already cover (so better use that than to re-invent the wheel).
    – Izzy
    Jun 23, 2016 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.