001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.utils.text; 015 016import java.util.regex.Matcher; 017import java.util.regex.Pattern; 018 019import org.apache.commons.lang3.StringUtils; 020 021public class EmailUtils { 022 023 public static class EmailWithName { 024 025 public String email; 026 public String name; 027 } 028 029 private static final Pattern EMAIL_PATTERN = 030 Pattern.compile("(?:(.+)(?: +| *<))?([^@ ]+(?:@| at )[^@ ]+\\.[a-zA-Z0-9_-]{2,4})(?: *>)?"); 031 032 public static EmailWithName parseEmail(String x) { 033 if (StringUtils.isBlank(x)) { 034 return null; 035 } 036 EmailWithName n = new EmailWithName(); 037 Matcher m = EMAIL_PATTERN.matcher(x); 038 if (m.find()) { 039 // int all = m.groupCount(); 040 // int idx = 0; 041 // while (idx<=all){ 042 // System.out.println(m.group(idx)); 043 // idx++; 044 // } 045 n.name = StringUtils.trimToNull(m.group(1)); 046 n.email = StringUtils.trimToNull(m.group(2)); 047 } else { 048 n.name = StringUtils.trimToNull(x); 049 } 050 return n; 051 } 052 053 private EmailUtils() { 054 throw new UnsupportedOperationException("Can't initialize class"); 055 } 056}