FAQ: Introduction to Regular Expressions - Review

well, now, after reading all your cool solutions, i feel super awkward to share mine, but here we go:

.(\s)?\d(.){1,7}(.?)*

:grimacing:

1 Like

((?\d{3})?|\d)(-| |.)?\d{3}(-| |.)?(\d{4}|\d{3} \d{4})

This worked and I’m too happy about it and I’m not sure why!!

1 Like

I solved the last challenge with:

.?\d.*

At first I only had (\d.)* and couldn’t figure out how to include the phone number with parentheses, then I figured it out.
Switch the \d to \w to get all the strings.
Use [a-z].* to get only the alpha strings.

1 Like

Look upon my answer and weep:

(?\d ?\d\d)?.?-?\d ?\d\d-?.?\d ?\d\d\d*

1 Like

I enjoy several very simplified solutions posted here. They’re fantastic.

Here’s mine:
[0-9(\s-).]+

\d? ?(?\d{3}(-| |)|.)\d{3}(-| |.)\d{1,4}

:ok_hand:

I used this regex ; but , I’m realizing there will be a wide variance in realm of acceptable answers LOL!! : \w?.\d.*

1?.?\d{3}.?\d{3}.?\d{4}

This works to find all USA phone numbers, provided that no two consecutive non-numeric symbols appear in the string. This is also restrictive enough that there is a very low likelihood of the regex picking up noise when applied to a relevant dataset.

The practical down side of your solution is that it would return sequences that are not phone numbers such as “Hello, world!” or “12345!@#$%”…essentially any pattern with a digit, a non-word character, and the length between 10 and 14. Correct?

Looking for the most robust regular expression to match US phone numbers I came up with: ^1? ?(?\d{3})?[-.\s]?(\d{3})[-.\s]?(\d{4})$