title: Regular expression matching E.164 formatted phone numbers
body: <p>I need to add a regular expression that matches all possible valid E.164 formatted phone numbers. </p>
  
  <p>This regex works fine for for North American phone numbers, but I need something that will work for international numbers as well:</p>
  
  <blockquote>
    <p>^(+1|1)?([2-9]\d\d[2-9]\d{6})$</p>
    
    <p>Example: +13172222222 matches
             13172222222 still matches because +1 or 1 are optional
             3172222222 still matches because +1 or 1 are optional
             3171222222 does not match and is not a valid NANPA number.</p>
  </blockquote>
  
  <p>Source: <a href="http://wiki.freeswitch.org/wiki/Regular_Expression" rel="noreferrer">Freeswitch.org</a></p>
  
  <p>I also came across <a href="https://stackoverflow.com/questions/2113908/what-regular-expression-will-match-valid-international-phone-numbers">this related question</a>, but I think that it is way too crazy for my needs. In my case, I am simply validating an entry for a blacklist, which I'm comparing to incoming Twilio data; so, I care much less about weather a country code is valid. I really only need to test if a number matches the general <a href="http://en.wikipedia.org/wiki/E.164" rel="noreferrer">E.164</a> form, rather than assuming it's a NANPA.</p>
  
  <p>To be better understand what I need to match, here is an example from the <a href="https://www.twilio.com/docs/api/rest/response#data-formats" rel="noreferrer">Twilio Documentation</a>:</p>
  
  <blockquote>
    <p>All phone numbers in requests from
    Twilio are in E.164 format if
    possible. For example, (415) 555-4345
    would come through as '+14155554345'.
    However, there are occasionally cases
    where Twilio cannot normalize an
    incoming caller ID to E.164. In these
    situations Twilio will report the raw
    caller ID string.</p>
  </blockquote>
  
  <p>I want to match something like +14155554345, but not (415) 555-4345, 415555434, 555-4345 or 5554345. The regex should not restrict itself to only matching the US country code though. Basically, it should match the +xxxxxxxxxxx format. I also think the number could be longer, as there are multi-digit country codes, such as in the UK. T-Mobile's UK number is +447953966150 I'll update this if I can come up with a better example.</p>