LocalPartRegexCheckerImpl extends AbstractEmailChecker { String translateToRegex(String localPart) { if (localPart == null) { return null; } String[] loacalPartSplits = StringUtils.split(localPart, Constant.LEFT_BRACKET); StringBuilder buf = new StringBuilder(); for (String loacalPartSplit : loacalPartSplits) { String splitedStr = loacalPartSplit; int indexOf = splitedStr.indexOf(Constant.RIGHT_END); if (indexOf != -1) { String startStr = splitedStr.substring(0, indexOf); String endStr = splitedStr.substring(indexOf + 1); endStr = replaceChar(endStr); splitedStr = startStr + endStr; } else { splitedStr = replaceChar(splitedStr); } buf.append(splitedStr); } return buf.toString(); } LocalPartRegexCheckerImpl(String regex, boolean isShort, boolean isCaseSensitive); @Override boolean check(String email);  }
@Test public void testTranslateToRegex() { localPartCheck = new LocalPartRegexCheckerImpl("", true, false); String regularPattern = "w.<tal.end>"; String convertedPattern = localPartCheck.translateToRegex(regularPattern); String expectedRegex = "[a-z]+\\.tal.end"; assertEquals(expectedRegex, convertedPattern); regularPattern = "<tal>w<end>"; convertedPattern = localPartCheck.translateToRegex(regularPattern); expectedRegex = "tal[a-z]+end"; assertEquals(expectedRegex, convertedPattern); regularPattern = "<tal>?<end>*"; convertedPattern = localPartCheck.translateToRegex(regularPattern); expectedRegex = "tal[^\\s\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]end[^\\s\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]*"; assertEquals(expectedRegex, convertedPattern); regularPattern = "<tal>?<+end>*"; convertedPattern = localPartCheck.translateToRegex(regularPattern); expectedRegex = "tal[^\\s\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+end[^\\s\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]*"; assertEquals(expectedRegex, convertedPattern); regularPattern = "<tal>?<\\+end>*"; convertedPattern = localPartCheck.translateToRegex(regularPattern); expectedRegex = "tal[^\\s\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]\\+end[^\\s\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]*"; assertEquals(expectedRegex, convertedPattern); regularPattern = "a<[._-]?>w"; convertedPattern = localPartCheck.translateToRegex(regularPattern); expectedRegex = "[a-z][._-]?[a-z]+"; assertEquals(expectedRegex, convertedPattern); regularPattern = "a.<[._w-]?>w<tat?>"; convertedPattern = localPartCheck.translateToRegex(regularPattern); expectedRegex = "[a-z]\\.[._w-]?[a-z]+tat?"; assertEquals(expectedRegex, convertedPattern); }