正则表达式在字符上转义

时间:2011-12-23 22:14:40

标签: java regex

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

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

"Talboom, Esther"

"Wolde, Jos van der"

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

最后一个出错了因为这个名字没有逗号,所以它一直持续到它找到一个,那就是我想用来分隔的那个。所以我希望它看起来直到找到'&lt;'。 我怎么能这样做?

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>";

1 个答案:

答案 0 :(得分:2)

我会使用String.splitString.replaceAll来简化代码。这避免了使用Pattern的麻烦,并使代码整洁而简洁 试试这个:

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