标签:style http color io java ar strong div sp
它包括两个类:Pattern和Matcher 。
Pattern: 创建匹配模式字符串。
Matcher:将匹配模式字符串与输入字符串。
? ?
package com.test; ? ? import java.io.*; import java.net.*; import java.util.regex.*; ? ? public class baidulogo { ????static String getUrlContentString(String urlString) throws Exception { ????????String result = ""; ????????URL url = new URL(urlString); ????????URLConnection urlConnection = url.openConnection(); ????????urlConnection.connect(); ????????InputStreamReader inputStreamReader = new InputStreamReader( ????????????????urlConnection.getInputStream(), "utf-8"); ????????BufferedReader in = new BufferedReader(inputStreamReader); ????????String line; ????????while ((line = in.readLine()) != null) { ????????????result += line; ????????} ????????return result; ????} ? ? ????static String getLogoUrl(String contentString, String patternString) { ????????String LogoUrl = null; ????????Pattern pattern = Pattern.compile(patternString); ????????Matcher matcher = pattern.matcher(contentString); ????????if (matcher.find()) { ????????????LogoUrl = matcher.group(1); ????????} ????????return LogoUrl; ? ? ????} ? ? ????public static void main(String[] args) throws Exception { ????????// 定义即将访问的链接 ????????String urlString = "http://www.baidu.com"; ????????String result = getUrlContentString(urlString); ????????String patternString = "src=\"(.+?)\""; ????????String contentString = result; ????????String logoUrl = getLogoUrl(contentString, patternString); ????????System.out.println(logoUrl); ????} } |
?
标签:style http color io java ar strong div sp
原文地址:http://www.cnblogs.com/keedor/p/3989762.html