Dren Dren - 4 months ago 11
Perl Question

capture alphanumeric string using regex

I have a file that contain any of the following strings

"155"
>555.123.4567
>555-123-4567
>(555).123.4567
>(555)123-4567
>(555)-123-4567


I would like to capture the strings except the 1st with the output like below using regex

555.123.4567
555-123-4567
(555).123.4567
(555)123-4567
(555)-123-4567


So far I am only able to come up with the regex below but it work only to the last three strings

(\([\d]+\).\-?(-|)[\d]+.-?[\d]+)/g

Answer

You can use this regex with optional delimiters to match your inputs:

/[(]?\d{3}[)]?[.-]?\d{3}[.-]?\d{4}\b/

RegEx Demo