Regex for phone numbers

I give up! I need a Regex to get just the area code and phone number but not the country code.

My data looks like this:
7781234567
But it can also look like this:
17781234567

I want to capture 7781234567

The first example is not a problem but the second example I just want to capture the rest of the phone number but not the “1”. And it is just “1” I want to get rid of. There are no other numbers in my data set that I want to remove from the front. And of course I want “1” if it is part of the phone number but not the country code. Plus there are no “-“ or spaces. My data looks exactly like I have it here. I promised I tried looking it up on the Internets but just could not find the right solution.

The only thing I can think of is trying to compare the beginnings of the phone numbers with a list like this one on wikipedia (maybe pared down specifically for your data set). But that can’t be done with a regex and without leading zeros it still might be impossible. Of course I don’t know the specifics of your country’s phone number system.

Sorry. Not making myself clear. The only number ahead of phone numbers I have are “1”. Most of the phone numbers don’t have a leading “1” but when my JavaScript comes across the phone numbers that do have a leading “1” I want to get rid of the “1” and keep the rest of the phone number. I could just split the numbers and have an if statement that says if the the first number begins with a one then just get the rest of the split numbers and combine them again. But I would prefer Regex solution. I should also add none of the phone numbers begin with a “1” except those that include the country code. So a Regex that gets rid of all leading “1”s would do the trick.

So all your phone numbers are in North America?

Correct. I just want to get rid of the “1” if it should appear in my data set.

In fact. The phone numbers begin with either “778” or “604”. But some begin “1778” or “1604”.

I feel like this should be dead easy in Regex but I’m flummoxed.

Haven’t used Regex in Scriptable, but does something like /1?(\d{10})/ achieve what you’re after?

Assumes that the numbers are all 10 digits, so would need to use something else if that’s not the case.

1 Like

Yep. That did the trick. Thank you.