如何使用jsoup提取电子邮件ID?
how to extract email id using jsoup?
Elements elements = doc.select("span.st");
for (Element e : elements) {
out.println("<p>Text : " + e.text()+"</p>");
}
元素 e
包含带有一些电子邮件 ID 的文本。如何从中提取 maild
id。我看过提供 :matches(regex)
的 Jsoup API 文档,但我不明白如何使用它。我正在尝试使用
^[a-zA-Z0-9_!#$%&’*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$
我在谷歌搜索时发现的。
在此先感谢您的帮助。
:matches(regex)
如果您想根据指定的正则表达式查找某些内容(例如,查找包含电子邮件的所有节点)。
我想这不是你想要的。 Instead, you need to extract the email from e.text()
using regex。在你的情况下:
Elements elements = doc.select("span.st");
for (Element e : elements) {
out.println("<p>Text : " + e.text()+"</p>");
out.println(extractEmail(e.text()));
}
// ...
public static String extractEmail(String str) {
Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0- 9-.]+").matcher(str);
while (m.find()) {
return m.group();
}
return null;
}
Elements elements = doc.select("span.st");
for (Element e : elements) {
out.println("<p>Text : " + e.text()+"</p>");
}
元素 e
包含带有一些电子邮件 ID 的文本。如何从中提取 maild
id。我看过提供 :matches(regex)
的 Jsoup API 文档,但我不明白如何使用它。我正在尝试使用
^[a-zA-Z0-9_!#$%&’*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$
我在谷歌搜索时发现的。
在此先感谢您的帮助。
:matches(regex)
如果您想根据指定的正则表达式查找某些内容(例如,查找包含电子邮件的所有节点)。
我想这不是你想要的。 Instead, you need to extract the email from e.text()
using regex。在你的情况下:
Elements elements = doc.select("span.st");
for (Element e : elements) {
out.println("<p>Text : " + e.text()+"</p>");
out.println(extractEmail(e.text()));
}
// ...
public static String extractEmail(String str) {
Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0- 9-.]+").matcher(str);
while (m.find()) {
return m.group();
}
return null;
}