java – 正则表达式在字符上转义

我必须分开一大堆电子邮件和名字,我必须用逗号分隔,但有些名字中有逗号,所以我必须首先处理它.幸运的是,这些名字介于“引号”之间.

目前我得到了这样的正则表达式输出(编辑:它在我看到的论坛中不显示电子邮件!):

"Talboom, Esther"

"Wolde, Jos van der"

"Debbie Derksen"  <deberken@casema.nl>,   corine  <corine5@xs4all.nl>, "

最后一个出错了,因为这个名字没有逗号,所以它一直持续到它找到一个,那就是我想用来分隔的那个.所以我希望它看起来直到找到’<‘.
我怎样才能做到这一点?

import java.util.regex.Pattern;
import java.util.regex.Matcher;

String test = "\"Talboom, Esther\" <E.Talboom@wegener.nl>,     \"Wolde, Jos van der\" <J.vdWolde@wegener.nl>,   \"Debbie Derksen\"  <deberken@casema.nl>,   corine  <corine5@xs4all.nl>, \"Markies Aart\" <A.Markies@wegenernieuwsmedia.nl>";

Pattern pattern = Pattern.compile("\".*?,.*?\"");

Matcher matcher = pattern.matcher(test);

boolean found = false;
while (matcher.find ()) {
  System.out.println(matcher.group());
}

编辑:
更好的工作线,因为并非所有都有名称或引号:

String test = "\"Talboom, Esther\" <E.Talboom@wegener.nl>,     DRP - Wouter Haan <wouter@drp.eu>, \"Wolde, Jos van der\" <J.vdWolde@wegener.nl>,   \"Debbie Derksen\"  <deberken@casema.nl>,   corine  <corine5@xs4all.nl>, clankilllller@gmail.com, \"Markies Aart\" <A.Markies@wegenernieuwsmedia.nl>";

最佳答案 我将使用String.split和String.replaceAll简化代码.这避免了使用模式的麻烦,并使代码整洁和简洁.

试试这个:

public static void main(String[] args) {
    String test = "\"Talboom, Esther\" <E.Talboom@wegener.nl>,     \"Wolde, Jos van der\" <J.vdWolde@wegener.nl>,   \"Debbie Derksen\"  <deberken@casema.nl>,   corine  <corine5@xs4all.nl>, \"Markies Aart\" <A.Markies@wegenernieuwsmedia.nl>";

    // Split up into each person's details
    String[] nameEmailPairs = test.split(",\\s*(?=\")");
    for (String nameEmailPair : nameEmailPairs) {
        // Extract exactly the parts you need from the person's details
        String name = nameEmailPair.replaceAll("\"([^\"]+)\".*", "$1");
        String email = nameEmailPair.replaceAll(".*<([^>]+).*", "$1");
        System.out.println(name + " = " + email);
    }
}

输出,显示它实际上工作:)

Talboom, Esther = E.Talboom@wegener.nl
Wolde, Jos van der = J.vdWolde@wegener.nl
Debbie Derksen = corine5@xs4all.nl
Markies Aart = A.Markies@wegenernieuwsmedia.nl
点赞