1

I want to write a regular expression to tack a number in Google Analytics for a specific US phone number that supports the following formats: ###-###-#### (###) ###-#### ### ### #### ###.###.#### where # means a specific number.

I need it written so that I can track a different phone number in the future by just swapping out the digits as needed.

So far I came up with this to validate a phone number that is in the different formats I'm looking for, but I'm having trouble understanding how to change it in order to validate a specific phone number, not just a general one. How would I track the phone (630) 321-4321 specifically for example?

This is how far I got:

^\+?\d{0,3}\s?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$

Like I mentioned above, this will validate a general phone number but I need a specific number, not a general one.

migrated from superuser.com Mar 9 '18 at 21:58

This question came from our site for computer enthusiasts and power users.

  • I'm trying to use for google analytics – Svet Slavov Mar 9 '18 at 14:59
0

I'd say:

^\(?\d{3}\)?[. -]\d{3}[. -]\d{4}$

Explaination:

  • ^: String start
  • \(?\d{3}\)?: That would match 3 numbers, with or without (). In this case the parentheses have a special meaning in regexp, so you have to escape them. Parentheses may or may not be in the expression, so that's why I placed the ? just after.
  • [. -]: One of these characters follow. Although the point has a special meaning in regexp, in defined in a set you don't have to escape it.
  • \d{3}: Just another 3 digits.
  • [. -]: Another separator. Either a ., or a -, or a space.
  • \d{4}: Just another 4 digits.
  • $: String end.
  • thanks, how would you modify this to match this number specifically: 123 456 7890 or (123) 456 7890 or (123) 456-7890 or 123.456.7890 – Svet Slavov Mar 9 '18 at 15:03
  • This expression should match all that cases already. Why do you want to modify it to match each of this expressions separately? – nKn Mar 9 '18 at 15:04
  • as I understand it, it is still a general match. It will match even if the is 321 123 1234. I need it to match only 123 456 7890 written in the different formats and no other phone number. – Svet Slavov Mar 9 '18 at 15:08
0

Since you're looking for only four formats, you might as well just build an OR search pattern with the four matching strings.

Search for:

(630 321 4321|630-321-4321|\(630\) 321-4321|630\.321\.4321)
  • thank you this is more of what I needed. I was just seeing if there was a simpler way of doing it. I'm very new to this so sorry if this sound ignorant but do you think this format will work for google analytics? thank you for your help! – Svet Slavov Mar 9 '18 at 15:12
  • All the operators used in the pattern are supported by Google Analytics regex search, so it should work. See: support.google.com/analytics/answer/1034324?hl=en – Excellll Mar 9 '18 at 15:23
  • so i gave it a shot but it only validated it 2 formats: 630.321.4321 & (630) 321-4321 . It did not match (6303214321) or (630) 321 4321 or 630 321-4321. Any suggestions? – Svet Slavov Mar 9 '18 at 16:07
0

I think, I got it to work. I had to do it the long way but at least it matches all formats. If anyone find a shorter way to do this, or find any faults with it let me know.

(6301234567|630-123-4567|630.123.4567|630 123 4567|\(?630\)?.123.4567|\(?630\)?\s?1234567|)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy