我有以下文字。
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);
}
}
}
答案 0 :(得分:0)
如何:
emailString.split(">,")
这将是yeld:
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;
}
答案 1 :(得分:0)
这将解析像'Jhon,N,Edward,&lt; edward@sri.lk&gt;'这样的字符串。作为一个
(.*?>),
这将分别解析名称和电子邮件。例如:'Jhon,N,Edward'和'edward@sri.lk'
(.*?),\s*<([^>]*)>,
答案 2 :(得分:0)
拆分:
(?<=>),\s+
示例:
String[] parts = emailString.split("(?<=>),\\s+");
System.out.println(parts[2]); // prints "Thomas, F, Kevin, <kevin@sri.lk>"
的可视化
答案 3 :(得分: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());
}
}
}
它会起作用。