Java Regular crawl Email

Source: Internet
Author: User

Implementation ideas:

1. Use the Java.net.URL object to bind the address of a Web page on a network

2. Obtain a Httpconnection object by using the OpenConnection () method of the Java.net.URL object

3. Obtain the input stream object of the network file through the getInputStream () method of the Httpconnection object InputStream

4. Loop through each row of data in the stream and compile the regular expression by the pattern object with each line of characters to get an email address

Key code:

Package Cn.bdqn;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;public class Test {public                static void Main (string[] args) throws Exception {//Create a URL object url url=new url ("");                Open Connection URLConnection conn=url.openconnection ();                Set the connection network timeout unit to milliseconds conn.setconnecttimeout (1000*10);        Read the file in the specified network address by streaming operation BufferedReader Bufr=new BufferedReader (New InputStreamReader (Conn.getinputstream ()));                String Line=null; Matches the email's regular String regex= "[a-za-z0-9_-][email protected]\\w+\\. [A-z]+ (\\.[                a-z]+)? ";                Use the compile () method of the pattern to generate the pattern object, pattern p=pattern.compile (regex);                        while ((Line=bufr.readline ())!=null) {Matcher m=p.matcher (line);                while (M.find ()) {System.out.println (M.group ());            }                    }             }} 

Java Regular crawl Email

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.