{"id":62,"date":"2015-11-26T05:21:58","date_gmt":"2015-11-26T13:21:58","guid":{"rendered":"https:\/\/cloudinsidr.com\/content\/?p=62"},"modified":"2015-11-26T16:29:17","modified_gmt":"2015-11-27T00:29:17","slug":"tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex","status":"publish","type":"post","link":"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/","title":{"rendered":"Tip of the Day: How to Extract Domain Names from Email Addresses using Regular Expressions (RegEX)"},"content":{"rendered":"<p>Extracting domain names from email addresses with the help of\u00a0regular expressions takes just a nanosecond\u00a0once you have the formula. The formula is the key.<\/p>\n<p><!--more--><\/p>\n<p>This is how you let your text editor find (and &#8220;understand&#8221;) each email address as two groups of characters separated by the @ symbol:<\/p>\n<pre>^(\\S+)@(\\S+)<\/pre>\n<p>This is how you perform the replacement, removing everything except for the domain name (as defined by the second group of characters in the regex\u00a0above):<\/p>\n<pre>($2)<\/pre>\n<p>It&#8217;s that easy.<\/p>\n<figure id=\"attachment_65\" aria-describedby=\"caption-attachment-65\" style=\"width: 551px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_domains_from_email_addresses.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-65 size-full\" src=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_domains_from_email_addresses.png\" alt=\"RegEx: a regular expression to extract domain or host names from email addresses\" width=\"551\" height=\"374\" srcset=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_domains_from_email_addresses.png 551w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_domains_from_email_addresses-300x204.png 300w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/a><figcaption id=\"caption-attachment-65\" class=\"wp-caption-text\">A regular expression to extract domain or host names from email addresses (here in Notepad++)<\/figcaption><\/figure>\n<h3>Practical Use Cases<\/h3>\n<p>One notable use case: Your email client may have collected untold amounts of email addresses from spammers. Now instead of blocking them on the client level (after delivery to your sorry inbox), you may want to block them at the mail server level. That&#8217;s easy enough (for example, you could be using <a href=\"https:\/\/cloudinsidr.com\/content\/anti-spam-defense-in-postfix\/\">smtpd sender restrictions in Postfix<\/a>), but what if you wanted to block <em>all<\/em> senders from the offending domains? Exactly. You need to remove the user names in front of the @ sign, leaving only\u00a0the domain names in the file. That&#8217;s where a regular expression\u00a0can come in handy.<\/p>\n<p>One more\u00a0thing: how do you come up with\u00a0a regular expression of your own? By using\u00a0a really good RegEx editor like <a href=\"https:\/\/regex101.com\/\" target=\"_blank\">regex101.com<\/a> and testing your regular expression\u00a0on a sample of data.<\/p>\n<p>Assuming that you want to match any host name on each of the domains in your Postfix configuration and reject each\u00a0message that matches the sender&#8217;s address, you need to prefix each of the domain names\u00a0in your list with a dot, append a space and the REJECT directive.<\/p>\n<figure id=\"attachment_88\" aria-describedby=\"caption-attachment-88\" style=\"width: 553px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-88 size-full\" src=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam.png\" alt=\"RegEx for Postfix antispam restrictions\" width=\"553\" height=\"351\" srcset=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam.png 553w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam-300x190.png 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/a><figcaption id=\"caption-attachment-88\" class=\"wp-caption-text\">A regular expression to extract domain or host names from email addresses for smtpd client restrictions in Postfix: spam originating from those domains will be rejected<\/figcaption><\/figure>\n<p>Once this is done, you may end up with a list full of duplicates. You can sort it and remove the duplicates in Notepad++ using the module TextFX and\u00a0the following commands:<\/p>\n<ul>\n<li><em>&#8220;TextFX &gt; TextFX Tools &gt; + Sort outputs only UNIQUE (at column) lines&#8221; <\/em>activates the first\u00a0option;<\/li>\n<li><em>&#8220;TextFX &gt; TextFX Tools &gt; + Sort ascending&#8221;<\/em> activates the\u00a0second option;<\/li>\n<li><em>&#8220;TextFX &gt; TextFX Tools &gt; + Sort lines case insensitive at column&#8221;<\/em> performs the magic by\u00a0sorting all lines and removing duplicates.<\/li>\n<\/ul>\n<p>If you need to perform some other action, you can help yourself to another regular expression (just remember to pay attention to the character encoding in your file; different systems encode line breaks differently: <strong>LF<\/strong> on Linux\/Unix, <strong>CR\/LF<\/strong> on Windows and <strong>CR<\/strong> on OS X).<\/p>\n<p>Now you can use the resulting\u00a0list of spamming domains\u00a0to feed Postfix smtpd client restrictions through this parameter:<\/p>\n<pre>smtpd_client_restrictions =\r\n<\/pre>\n<p><a href=\"https:\/\/cloudinsidr.com\/content\/anti-spam-defense-in-postfix\/\">as described in this post<\/a>.<\/p>\n<p><strong>A word of caution<\/strong>, though, is in order: your email-address-list-<em>turned<\/em>-domain-list may include domain names of legitimate email service providers\u00a0such as gmail.com or yahoo.com. As a result of this configuration, you\u00a0would block all their users from communicating with your Postfix. This is bad for business. You may want to apply this technique <strong>only to slam-dunk cases of spamming domains<\/strong>, otherwise put <a href=\"https:\/\/cloudinsidr.com\/content\/anti-spam-defense-in-postfix\/\">smtpd sender restrictions in place<\/a>.<\/p>\n<figure id=\"attachment_87\" aria-describedby=\"caption-attachment-87\" style=\"width: 561px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam_FROM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-87 size-full\" src=\"https:\/\/cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam_FROM.png\" alt=\"RegEx for Postfix antispam restrictions\" width=\"561\" height=\"351\" srcset=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam_FROM.png 561w, https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/regex_for_postfix_antispam_FROM-300x188.png 300w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/a><figcaption id=\"caption-attachment-87\" class=\"wp-caption-text\">This RegEx appends the Postfix directive REJECT to full email addresses of spammers creating a config file for smtpd sender restrictions<\/figcaption><\/figure>\n<p>Either method will take a huge workload off of your MX server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extracting domain names from email addresses with the help of\u00a0regular expressions takes just a nanosecond\u00a0once you have the formula. The formula is the key.<\/p>\n","protected":false},"author":1,"featured_media":76,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[16,2],"tags":[32,31,29,30],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tip of the Day: How to Extract Domain Names from Email Addresses using Regular Expressions (RegEX) - CloudInsidr<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tip of the Day: How to Extract Domain Names from Email Addresses using Regular Expressions (RegEX) - CloudInsidr\" \/>\n<meta property=\"og:description\" content=\"Extracting domain names from email addresses with the help of\u00a0regular expressions takes just a nanosecond\u00a0once you have the formula. The formula is the key.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudInsidr\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-26T13:21:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-11-27T00:29:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/cloudinsidr_logo_900px-wide.png\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"326\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/\",\"name\":\"CloudInsidr\",\"description\":\"Cyber security, infotech\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.cloudinsidr.com\/content\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2015\/11\/cloudinsidr_logo_900px-wide.png\",\"width\":900,\"height\":326,\"caption\":\"cloudinsidr.com logo (900px wide)\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/#webpage\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/\",\"name\":\"Tip of the Day: How to Extract Domain Names from Email Addresses using Regular Expressions (RegEX) - CloudInsidr\",\"isPartOf\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/#primaryimage\"},\"datePublished\":\"2015-11-26T13:21:58+00:00\",\"dateModified\":\"2015-11-27T00:29:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/dd6ee9cb21cf05763fd7cff3d6f11b2b\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.cloudinsidr.com\/content\/tip-of-the-day-how-to-extract-domain-names-from-email-addresses-using-regular-expressions-regex\/\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/dd6ee9cb21cf05763fd7cff3d6f11b2b\",\"name\":\"Cloud Insidr\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8b2fa1415b3d573b97d818b8f8f83b7c?s=96&d=mm&r=g\",\"caption\":\"Cloud Insidr\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":19,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/62\/revisions\/96"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media\/76"}],"wp:attachment":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}