Detect the Email Addresses

Sort by

recency

|

168 Discussions

|

  • + 0 comments

    JavaScript

    I made it so complex, even I got confused.

    function processData(input) {
        let criteria = /([a-z\d._]+)@([a-z\d._]+\.[a-z]+)/gi;
        let arr = input.match(criteria);
        let emails = [...new Set(arr)];
        console.log(emails.sort().join(";"));
    }
    

    Corrected regex: let criteria = /[\w._]+@[\w._]+[\w]/g;

  • + 0 comments

    Java 15

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt(); scanner.nextLine();
            // Before and After @: (word.word.word...), with atleast one word, where each word is \w+
            Pattern pattern = Pattern.compile("\\w+(\\.\\w+)*@\\w+(\\.\\w+)*"); 
            // Creating hashset to hold unique emails
            HashSet<String> hashset = new HashSet<String>();
            for (int i=0;i<n;i++) {
                   Matcher matcher = pattern.matcher(scanner.nextLine());
                   while(matcher.find())    hashset.add(matcher.group());
            }
            // Converting hashset to arraylist in order to use sort()
            ArrayList arraylist = new ArrayList<String>(hashset);
            // Sorting arraylist in lexicographical order
            Collections.sort(arraylist);
            // Using ListIterator to iterate through arraylist
            ListIterator<String> iterator = arraylist.listIterator();
            while(iterator.hasNext()) {
                System.out.print(iterator.next());
                if(iterator.hasNext())  System.out.print(";");
            }
        }
    }
    
  • + 1 comment

    Python 3

    import re
    import sys
    n = int(input())
    string = sys.stdin.read()
    
    pattern = r'((\w+(\.\w+)*)@(\w+(\.\w+)*))'
    matches = re.finditer(pattern, string)
    email = set()
    for match in matches:
        email.add(match.group(0))
    print(";".join(sorted(list(email)))
    
  • + 0 comments
    import re, sys
    
    regex = re.findall(r"[\w.]+@\w+(?:\.\w+)+", sys.stdin.read())
    print(*sorted(set(regex)), sep=';')
    
  • + 0 comments

    import re pattern= re.compile(r'\b[\w.]+@[a-zA-Z0-9.]+\w\b') n=input() email=[] for i in range(eval(n)): text=input() add=pattern.findall(text) if add: email.extend(add) set_email=list(set(email)) set_email.sort() string=';'.join(set_email) print(string)