I did my code for regular expression that selects lines containing a phone number. I have to create an expression for a phone number, but it may have – or . in between the numbers, and it may or may not have an extension. I couldn’t figure out and didn’t submitted as my homework…I just want to know how its done right because I might have it on the Exam at the end of the semester.
718-951-5000,
718.951.5000,
(718)-951-5000
My code
"(^(\([2-9]\\d{2}\)|[2-9]\\d{2})[- .]?\d{3}[- .]?\\d{4}$)|(^(\([2-9]\\d{2}\)|[2-9]\\d{2})[- .]?\d{3}[- .]?\\d{4}[xX]\\d{1,4}$)"
My Error
RETest1.java:4: error: illegal escape character
"(^(\([2-9]\\d{2}\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$)|(^(\([2-9]\\d{2}\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}[xX]\\d{1,4}$)"
^
[- .]?\d{3}[- .]?You forgot the second escape
\on the\dShould be:
"(^(\([2-9]\\d{2}\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$)|(^(\([2-9]\\d{2}\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}[xX]\\d{1,4}$)"