VBA使用模式验证电子邮件列表

时间:2017-05-24 16:25:40

标签: excel vba email design-patterns

我有一个由分号和空格分隔的单元格中的电子邮件列表,但我有一个模式,只有当它们被分号分隔时才验证电子邮件。我在哪里插入空间以适应我的模式?

电子邮件示例: aaa@bbb.com; ccc@eee.com; fff@ggg.com

单个单元格中的电子邮件地址数量从1到20不等

模式验证由分号分隔的电子邮件:

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class HttpRetryTest{


      @Autowired
      @Qualifier("retryTemplate_test")
      RetryOperations retryTemplate_test;

    @Test
    public void testRetry() throws Throwable {
        Map<Class<? extends Throwable>, Boolean> r = new HashMap<>();
        r.put(HttpStatusCodeException.class, true);

            MockRetryCallback callback = new MockRetryCallback();
            MockRetryCallback.attemptsBeforeSuccess =5;            
            retryTemplate_test.execute(callback);

            assertEquals(5, MockRetryCallback.attempts);        
    }

    private static class MockRetryCallback implements RetryCallback<Object, HttpStatusCodeException> {

        private static int attempts;

        private static int attemptsBeforeSuccess;


        @SuppressWarnings("serial")
        @Override
        public Object doWithRetry(RetryContext status) throws HttpStatusCodeException {
            MockRetryCallback.attempts++;
            if (MockRetryCallback.attempts <= MockRetryCallback.attemptsBeforeSuccess) {
                System.out.println("I'm here: "+ MockRetryCallback.attempts);
                throw new HttpStatusCodeException(HttpStatus.INTERNAL_SERVER_ERROR) {
                };

            }
            return null;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

喜欢这个?
https://github.com/sevagh/stdcap/blob/master/stdcap.go

^\s?(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$

答案 1 :(得分:1)

你使你的生活变得比它需要的复杂得多。

Dim addresses 'As Variant
addresses = Split(addresses, ";")

完成。你有一个数组的地址(虽然有一个额外的空间,但你可以轻松修剪它)。正则表达式将不可避免地排除有效的电子邮件,除非它符合电子邮件地址的RFC822标准和you don't want such a regex pattern in your code

  

RFC 822中描述的语法非常复杂。使用正则表达式实现验证在某种程度上推动了使用正则表达式[...]

的合理范围。