Java中URL类和正则表达式


URL类(Package java.net

  主要用于将用户所输入的网络地址进行分割。

  主要方法:1.构造方法:public URL​(String spec) throws MalformedURLException

       2.public String getFile();//获取文件名;

       3.public int getPort();//获取端口;

       4.public String getProtocol();//获取协议;

       5.public String getHost();//获取主机;

       6.public URLConnection openConnection() throws IOException;//获取URLConnection对象,该对象对Socket类又进行了封装。

  代码示例:使用以上方法;

package second.study;

import java.net.MalformedURLException;
import java.net.URL;

public class Test {

    public static void main(String[] args) throws Exception {
        URLDemo();
    }
    private static void URLDemo() throws MalformedURLException {
        URL url = new URL("http://192.168.2.158:10002/myweb/Demo.html?name=haha");
        System.out.println("getProtocol = " + url.getProtocol());
        System.out.println("getPath = " + url.getPath());
        System.out.println("File = " + url.getFile());
        System.out.println("getQuery = " + url.getQuery());
    }
}
/* Res:
 * getProtocol = http
 * getPath = /myweb/Demo.html
 * File = /myweb/Demo.html?name=haha
 * getQuery = name=haha
 */

URLConnectionPackage java.net

  将Socket对象进行了封装,并对收到的传输层数据进行拆包到应用层。简化代码的书写。

  

package second.study;

import java.io.IOException;
import java.net.URL;

public class Test {

    public static void main(String[] args) throws Exception {
        openConnection();
    }
    private static void openConnection() throws IOException {
        URL url = new URL("http://192.168.2.158:10002/myweb/Demo.html?name=haha");
        System.out.println("openConnection = " + url.openConnection());

    }
}
/* Res:
 * openConnection = 
 * sun.net.www.protocol.http.HttpURLConnection:http://192.168.2.158:10002/myweb/Demo.html?name=haha
*/

正则表达式

  符合一定规则的表达式。专门用于操作字符串,并简化对字符串的复杂操作。注意:字符串中一旦出现\,就要出现两次(\\)。

  为了让规则的结果能被重用,可以按照将该规则封装成一个组,用()完成,每个组都有编号,从1开始,想要使用已有的组可以通过\n(n就是组的编号)的形式来获取。

  $n:获取前一个正则表达式中的第n组。

  具体操作功能:

    匹配:String类中的:public boolean matches​(String regex);

    切割:String类中的:public String[] split​(String regex);

    替换:String类中的:public String replaceAll​(String regex, String replacement);

    获取:将字符串中符合规则的子串取出。

      步骤:1.将正则表达式封装成对象;

         2.让正则对象和要操作的字符串相关联;

         3.关联后,获取正则匹配引擎;

         4.通过引擎对符合规则的子串进行操作,比如取出。

 Class Pattern(Package java.util.regex

    主要方法:1.public static Pattern compile​(String regex);//将正则表达式封装成对象

          2.public Matcher matcher​(CharSequence input);//将该对象与字符串关联,返回匹配器;

         3.Matcher类中的public boolean find();//寻找匹配内容;

         4.Matcher类中的public String group();//获取匹配内容;//find和group方法相当于迭代器。

网络爬虫简单代码示例

package second.study;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 需求:获取指定互连网上的邮件地址。
 * 思路:
 *     函数1:getInputStream获取指定浏览器中的数据:返回InputStream
 *         1.使用URL对象连接上指定的互联网;
 *         2.使用URL对象中的URLConnection返回URLConnection应用层对象;
 *         3.使用URLConnection对象中的InputStream获取该浏览器中的输入流获取文字;
 *     函数2:将字节流转换为字符流,并和相应的正则表达式匹配,存入TreeSet集合,避免重复字符串。
 *         1.使用bufferedReader InputStreamReader 对象,将字节流转换为字符流;
 *         2.使用Pattern对象将正则表达式进行编译,和输入流进行关联,返回匹配器对象
 *
 */
public class Test {

    public static void main(String[] args) throws Exception {
        getMails();
    }
    private static void getMails() throws IOException {
        InputStream inputStream = getInputStream("https:****");
        TreeSet<String> set = getSetFromIPAdd(inputStream);
        for(String str : set) {
            System.out.println(str);
        }
    }
    /**
     * 将匹配的结果存入到集合中
     * @param inputStream
     * @return
     * @throws IOException 
     */
    private static TreeSet<String> getSetFromIPAdd(InputStream inputStream) throws IOException {
//        1.创建集合
        TreeSet<String> set = new TreeSet<String>();
//        创建正则表达式
        String regex = "\\w+@\\w+(\\.\\w+)+";
//        将正则表达式编译
        Pattern pattern = Pattern.compile(regex);
//        将字节流转换为字符流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String len = null;
        while((len = bufferedReader.readLine()) != null) {
//            将字符流和正则表达式关联
            Matcher matcher = pattern.matcher(len);
//            运行匹配器
            while(matcher.find()) {
                set.add(matcher.group());
            }
        }
//        关闭流资源
        inputStream.close();
        return set;
    }
    /**
     * 连接互联网指定的InetAdd地址,返回连接成功后的输入流。
     * @param InetAdd : 互联网地址;
     * @return 输入流
     * @throws IOException
     */
    private static InputStream getInputStream(String InetAdd) throws IOException {
//        1.使用URL对象连接上指定的互联网;
        URL url = new URL(InetAdd);
//        2.使用URL对象中的URLConnection返回URLConnection应用层对象;
        URLConnection connection = url.openConnection();
//        3.使用URLConnection对象中的InputStream获取该浏览器中的输入流获取文字;
        InputStream inputStream = connection.getInputStream();
        return inputStream;
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM