我在一个servlet中获取电子邮件地址列表,作为from request中的一个参数,格式如下:
,Group 4: [abc@xyz.com,asd@dsa.com],,Group 4: [abc@xyz.com],,Group 3: [],,Group 2:
[qwe@rty.com,yui@gui.com,jih@app.com,abc@xyz.com,asd@dsa.com],,Group 1:
[pick@pick.com,test@pick.com,test1@pick1.com],,Nirmal testGroup: [qwe@rty.com],如何在Java中解析所有唯一的电子邮件地址?
组名并不重要。此外,组名称不一定总是组1、组3,它可以是任何包含空格的名称。只需要有一个列表/数组的所有唯一的电子邮件地址从字符串。
发布于 2011-08-06 12:59:13
使用regex挑出方括号([])之间的所有内容,然后对逗号中的所有内容执行split操作:
String example = ",Group 4: [abc@xyz.com,asd@dsa.com],,Group 4: [abc@xyz.com],,Group 3: [],,Group 2:\n" +
"[qwe@rty.com,yui@gui.com,jih@app.com,abc@xyz.com,asd@dsa.com],,Group 1: \n" +
"[pick@pick.com,test@pick.com,test1@pick1.com],,Nirmal testGroup: [qwe@rty.com],";
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher matcher = pattern.matcher(example);
while (matcher.find()) {
for (String email : matcher.group(1).split(",")) {
System.out.println(email);
}
}https://stackoverflow.com/questions/6964879
复制相似问题