Java regular expression (1). Capture web page email address instances

Source: Internet
Author: User

Implementation ideas:
1. Use a java.net. URL object to bind a webpage address on the network
2. Obtain an HttpConnection object through the openConnection () method of the java.net. URL object.
3. Use the getInputStream () method of the HttpConnection object to obtain the input stream object InputStream of the network file.
4. read each row of data in the stream cyclically, and use the regular expression compiled by the Pattern object to partition each row of characters to obtain the email address.
[Java]
Package regex;
 
Import java. io. BufferedReader;
Import java. io. InputStreamReader;
Import java.net. URL;
Import java.net. URLConnection;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
 
/**
* Web crawlers capture the email addresses on the webpage
*/
Public class WebCrawlersDemo {

Public static void main (String [] args) throws Exception {
URL url = new URL ("http://www.tianya.cn/publicforum/content/english/1/129176.shtml ");
// Open the connection
URLConnection conn = url. openConnection ();
// Set the network connection timeout.
Conn. setConnectTimeout (1000*10 );
// Read files from the specified network address
BufferedReader bufr = new BufferedReader (new InputStreamReader (conn. getInputStream ()));
String line = null;
String regex = "[a-zA-Z0-9 _-] + @ \ w + \. [a-z] + (\. [a-z] + )? "; // Match the regular expression of the email
Pattern p = Pattern. compile (regex );
While (line = bufr. readLine ())! = Null ){
Matcher m = p. matcher (line );
While (m. find ()){
System. out. println (m. group (); <span style = "white-space: pre"> </span> // obtain the matching email
}
}
}
 
}
Result:
Author: xyang81

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.