{"id":127,"date":"2023-08-01T08:36:41","date_gmt":"2023-08-01T14:36:41","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=127"},"modified":"2023-08-02T14:41:54","modified_gmt":"2023-08-02T20:41:54","slug":"regex-for-emails","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/regex-for-emails\/","title":{"rendered":"Regex for finding emails using grep (Linux)"},"content":{"rendered":"\n<p>Sometimes, we do need to find emails in a large file sized base, easily, from a terminal, without having to use multiple tools. <\/p>\n\n\n\n<p>Linux, offers <code>grep<\/code>, a powerful tool that allows to easily find certain specific regex expressions. <\/p>\n\n\n\n<p>Let say for example, need to find emails, based on text analisis in a text file named &#8220;emails.txt&#8221;, the code will be as follows:<\/p>\n\n\n\n<p><code>grep -P '^[\\w._]{5,30}(+[\\w]{0,10})?@[\\w.-]{3,}?.\\w{2,5}$' emails.txt\\<\/code><\/p>\n\n\n\n<p><strong>Explanation: <\/strong><\/p>\n\n\n\n<ol>\n<li><code>^<\/code>: start of a line.<\/li>\n\n\n\n<li><code>[\\w\\._]{5,30}<\/code>:  matches a word-like pattern of 5 to 30 characters (allows alphanumeric characters, underscores, and dots)<\/li>\n\n\n\n<li><code>(\\+[\\w]{0,10})?<\/code>:  optional part, it matches a plus sign (+) and then 0 to 10 word characters (parentheses and the question mark make it optional)<\/li>\n\n\n\n<li><code>@<\/code>: is the regular &#8220;@&#8221; character in email addresses.<\/li>\n\n\n\n<li><code>[\\w\\.\\-]{3,}?<\/code>:  matches the domain name in the email (allows word characters, dots, and hyphens, the domain name should be at least 3 characters) the question mark makes it a non-&#8220;greedy match&#8221;, meaning it will match the shortest possible domain name.<\/li>\n\n\n\n<li><code>\\.w{2,5}<\/code>: matches the top-level domain (TLD) such as &#8220;.com&#8221;, &#8220;.org&#8221;, or &#8220;.edu&#8221; (allows 2 to 5 word characters).<\/li>\n\n\n\n<li><code>$<\/code>: end of a line.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, we do need to find emails in a large file sized base, easily, from a terminal, without having to use multiple tools. Linux, offers grep, a powerful tool that allows to easily find certain specific regex expressions. Let say for example, need to find emails, based on text analisis in a text file named [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":244,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,9,4,6,7,8],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/127"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":4,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions\/175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/244"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}