java 正则 url_Java中URL类和正则表达式
URL类(主要用于将用户所输入的网络地址进行分割。主要方法:1.构造方法:2.publicStringgetFile();//获取文件名;3.publicintgetPort();//获取端口;4.publicStringgetProtocol();//获取协议;5.publicStringgetHost();//获取主机;6.publicURLConnectionopenCo...
URL类(
主要用于将用户所输入的网络地址进行分割。
主要方法:1.构造方法:
2.public String getFile();//获取文件名;
3.public int getPort();//获取端口;
4.public String getProtocol();//获取协议;
5.public String getHost();//获取主机;
6.public URLConnection openConnection() throws IOException;//获取URLConnection对象,该对象对Socket类又进行了封装。
代码示例:使用以上方法;
packagesecond.study;importjava.net.MalformedURLException;importjava.net.URL;public classTest {public static void main(String[] args) throwsException {
URLDemo();
}private static void URLDemo() throwsMalformedURLException {
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*/
URLConnection(Package java.net)
将Socket对象进行了封装,并对收到的传输层数据进行拆包到应用层。简化代码的书写。
packagesecond.study;importjava.io.IOException;importjava.net.URL;public classTest {public static void main(String[] args) throwsException {
openConnection();
}private static void openConnection() throwsIOException {
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(
主要方法: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方法相当于迭代器。
网络爬虫简单代码示例
packagesecond.study;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.URL;importjava.net.URLConnection;importjava.util.TreeSet;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/*** 需求:获取指定互连网上的邮件地址。
* 思路:
* 函数1:getInputStream获取指定浏览器中的数据:返回InputStream
* 1.使用URL对象连接上指定的互联网;
* 2.使用URL对象中的URLConnection返回URLConnection应用层对象;
* 3.使用URLConnection对象中的InputStream获取该浏览器中的输入流获取文字;
* 函数2:将字节流转换为字符流,并和相应的正则表达式匹配,存入TreeSet集合,避免重复字符串。
* 1.使用bufferedReader InputStreamReader 对象,将字节流转换为字符流;
* 2.使用Pattern对象将正则表达式进行编译,和输入流进行关联,返回匹配器对象
**/
public classTest {public static void main(String[] args) throwsException {
getMails();
}private static void getMails() throwsIOException {
InputStream inputStream= getInputStream("https:****");
TreeSet set =getSetFromIPAdd(inputStream);for(String str : set) {
System.out.println(str);
}
}/*** 将匹配的结果存入到集合中
*@paraminputStream
*@return*@throwsIOException*/
private static TreeSet getSetFromIPAdd(InputStream inputStream) throwsIOException {//1.创建集合
TreeSet set = new TreeSet();//创建正则表达式
String regex = "\\w+@\\w+(\\.\\w+)+";//将正则表达式编译
Pattern pattern =Pattern.compile(regex);//将字节流转换为字符流
BufferedReader bufferedReader = new BufferedReader(newInputStreamReader(inputStream));
String len= null;while((len = bufferedReader.readLine()) != null) {//将字符流和正则表达式关联
Matcher matcher =pattern.matcher(len);//运行匹配器
while(matcher.find()) {
set.add(matcher.group());
}
}//关闭流资源
inputStream.close();returnset;
}/*** 连接互联网指定的InetAdd地址,返回连接成功后的输入流。
*@paramInetAdd : 互联网地址;
*@return输入流
*@throwsIOException*/
private static InputStream getInputStream(String InetAdd) throwsIOException {//1.使用URL对象连接上指定的互联网;
URL url = newURL(InetAdd);//2.使用URL对象中的URLConnection返回URLConnection应用层对象;
URLConnection connection =url.openConnection();//3.使用URLConnection对象中的InputStream获取该浏览器中的输入流获取文字;
InputStream inputStream =connection.getInputStream();returninputStream;
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)