0

我有以下文字。

emailString = "Jhon, N, Edward, <edward@sri.lk>, " +
            "Mickal, Lantz, <mickal@sri.lk>, " +
            "Thomas, F, Kevin, <kevin@sri.lk>, " +
            "Marina, Anderson, <marina@sri.lk>, " +
            "Henry, Ford, <ford@sri.lk>, " +
            "Davin, Cammeron, <Cammeron@sri.lk>";

从上面的文字中,我想一一列出每个信息,如下所示。

Jhon, N, Edward, <edward@sri.lk>
Mickal, Lantz, <mickal@sri.lk>
Thomas, F, Kevin, <kevin@sri.lk>
Marina, Anderson, <marina@sri.lk>
Henry, Ford, <ford@sri.lk>
Davin, Cammeron, <Cammeron@sri.lk>

我尝试使用 Java 正则表达式来做到这一点,但没有成功。

如何在 Java 中使用正则表达式解决这个问题?

以下是我使用的示例类。

 public class MainFrame
    {

        private static final String emailString = "Jhon, N, Edward, <edward@sri.lk>, " +
                "Mickal, Lantz, <mickal@sri.lk>, " +
                "Thomas, F, Kevin, <kevin@sri.lk>, " +
                "Marina, Anderson, <marina@sri.lk>, " +
                "Henry, Ford, <ford@sri.lk>, " +
                "Davin, Cammeron, <Cammeron@sri.lk>";

        public MainFrame()
        {

        }
        /**
         * @param args
         */
        public static void main(String[] args) 
        {
        String regularExpression = "(([.])*([A-Za-z0-9])*([.*])*)*(<[a-z0-9-]+(\\.[a-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[a-z]{2,})>)([.])*([A-Za-z0-9])*([.*])*";
        Pattern pattern = Pattern.compile(regularExpression);

        Matcher matcher = pattern.matcher(emailString);

        String[] emails = emailString.split(regularExpression);

        for(String email : emails)
        {
            System.out.println("Email Address : " + email);
        }

        }

    }
4

4 回答 4

0

怎么样:

emailString.split(">,")

这将产生:

Jhon, N, Edward, <edward@sri.lk
 Mickal, Lantz, <mickal@sri.lk
 Thomas, F, Kevin, <kevin@sri.lk
 Marina, Anderson, <marina@sri.lk
 Henry, Ford, <ford@sri.lk
 Davin, Cammeron, <Cammeron@sri.lk>

正则表达式非常简单,但结果需要进一步处理:

  • 除了最后一个条目之外的每个条目都>从末尾剥离
  • 开头有空格

所有这些都可以通过以下方式解决:

String[] split = emailString.split(">,");
for (int i = 0; i < split.length; i++) {
    String string = split[i].trim();
    if(!string.endsWith(">"))
        string = string + '>';
    split[i] = string;
}
于 2013-11-15T10:05:43.487 回答
0
public class MainFrame {

    private static final String emailString = "Jhon, N, Edward, <edward@sri.lk>, " + "Mickal, Lantz, <mickal@sri.lk>, " + "Thomas, F, Kevin, <kevin@sri.lk>, " + "Marina, Anderson, <marina@sri.lk>, " + "Henry, Ford, <ford@sri.lk>, " + "Davin, Cammeron, <Cammeron@sri.lk>";

    /**
     * @param args
     */
    public static void main(String[] args) {
        String EMAIL_PATTERN = "[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";

        String regularExpression = "(.*?<)" + EMAIL_PATTERN + "(>,)";
        Pattern pattern = Pattern.compile(regularExpression);

        Matcher matcher = pattern.matcher(emailString);

        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

}

它会起作用的。

于 2013-11-15T10:40:19.480 回答
0

拆分方式:

(?<=>),\s+

正则表达式可视化

例子:

String[] parts = emailString.split("(?<=>),\\s+");
System.out.println(parts[2]); // prints "Thomas, F, Kevin, <kevin@sri.lk>"

Debuggex可视化

于 2013-11-15T10:15:04.313 回答
0

这会将像 'Jhon, N, Edward, <edward@sri.lk>' 这样的字符串解析为一个

(.*?>),

这将分别解析名称和电子邮件。例如:“Jhon, N, Edward”和“edward@sri.lk”

(.*?),\s*<([^>]*)>,
于 2013-11-15T10:10:21.523 回答