I have created a regex to match phone numbers in US format, it reads like
Pattern.compile("\\(?(\\d{3})\\)?-?\\s?(\\d{3})-(\\d{4})"), it does its job, but also matches itself with unwanted strings for e.g it returns 231-823-1255 for 103-3231823-1255288 when it actually shouldn’t, where Am I going wrong?
And yes I am writing a Java code…
You can use
$to tie the regex to the end of the string. See http://docs.oracle.com/javase/tutorial/essential/regex/bounds.html.