{"id":80286,"date":"2016-10-07T11:59:51","date_gmt":"2016-10-07T18:59:51","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80286"},"modified":"2019-02-18T09:10:25","modified_gmt":"2019-02-18T16:10:25","slug":"powertip-how-to-detect-phone-numbers-using-regular-expressions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-how-to-detect-phone-numbers-using-regular-expressions\/","title":{"rendered":"PowerTip: How to detect phone numbers using regular expressions"},"content":{"rendered":"<p><strong>Summary<\/strong>: Cloud and\u00a0Datacenter Management MVP, Thomas Rayner, shows how to use regex to detect if a string is a phone number.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> I have an array like <code>$phoneornot = @('780-123-4567', '780 321 6548', 'notme', 'this is 2 num')<\/code>, and I need to know which array items are phone numbers. How can I do this?<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> You can compare each item in the array with all its non-digit characters removed and see if that matches your requirements for\u00a0a phone number. Here in Canada, our phone numbers are 10 digits. Here is an example:<\/p>\n<p style=\"padding-left: 90px\"><code>$phones = [regex]::matches(($phoneornot -replace '\\D', ''),'\\b(\\d{10})\\b')<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>$phones.value<\/code><\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\"The Doctor\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Cloud and\u00a0Datacenter Management MVP, Thomas Rayner, shows how to use regex to detect if a string is a phone number. I have an array like $phoneornot = @(&#8216;780-123-4567&#8217;, &#8216;780 321 6548&#8217;, &#8216;notme&#8217;, &#8216;this is 2 num&#8217;), and I need to know which array items are phone numbers. How can I do this? You can [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,639,687,641],"tags":[56,356,652,45],"class_list":["post-80286","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-powertip","category-regular-expressions","category-windows-powershell","tag-guest-blogger","tag-powertip","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Cloud and\u00a0Datacenter Management MVP, Thomas Rayner, shows how to use regex to detect if a string is a phone number. I have an array like $phoneornot = @(&#8216;780-123-4567&#8217;, &#8216;780 321 6548&#8217;, &#8216;notme&#8217;, &#8216;this is 2 num&#8217;), and I need to know which array items are phone numbers. How can I do this? You can [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=80286"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80286\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=80286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}