class ProfileFromMailFromServiceSensor implements ProfileFromMailFromService {

    @Override
    public Profile generate(String mailFrom){

        String[] regexes = new String[]{
          "\"?([\\p{L}\\?-_]+)\\s?([\\p{L}\\?\\.\\d=\\\\ _-]+)?\"?\\s*<(.*)>", //  Alex Fiestas <afiestas@kde.org>
          "([\\p{L}\\.\\?-_]+) at ([\\p{L}\\?\\.\\d=\\-_]+) (\\((.*( .*)?)\\))?"
        };

        String regex = null;

        for(String reg:regexes){

            if(mailFrom.matches(reg)){
                regex = reg;
                break;
            }
        }

        if(regex == null){
            logger.error("Could not find a suitable regular expression for {} ",mailFrom);
            return null;
        }

        logger.debug("Found a match for {} ",mailFrom);

        /*
        We are making the following assumptions that we have
            1.) Email format Name Lastname <email>
            2.) Names are composed of a single word, the rest is the last name

            Alex Fiestas <afiestas@kde.org>
         */
        Pattern p = Pattern.compile(regex);

        int i = ArrayUtils.indexOf(regexes, regex);
        Profile profile = new Profile();

        Matcher matcher = p.matcher(mailFrom);
        matcher.matches();

        switch (i){
            case 0:   //  Alex Fiestas <afiestas@kde.org>

                if(matcher.groupCount() <= 2){
                    //single name
                    profile.setName(matcher.group(1).trim());
                    profile.setEmail(matcher.group(2).trim());
                    profile.setUsername(matcher.group(2).trim());
                }else if( matcher.groupCount()>2){
                    profile.setName(matcher.group(1).trim());
                    profile.setLastname(StringUtils.trimToEmpty(matcher.group(2)));
                    profile.setEmail(matcher.group(3).trim());
                    profile.setUsername(matcher.group(3).trim());
                }
                break;
            case 1:    //  afiestas at kde com
                profile.setEmail(matcher.group(1).trim()+" "+matcher.group(2).trim());
                profile.setName(StringUtils.substringBefore(matcher.group(4).trim()," "));
                profile.setLastname(StringUtils.defaultIfEmpty(StringUtils.substringAfter(matcher.group(4).trim()," ").trim(),""));
                break;
        }
        return profile;

    }

}