Ⅰ java 正則表達式獲取字元串中的URL和標題
String s = "<movie title=\"電影1\" src=\"https://www.qq.com:808/asdsadsd+_#$\"/> <movie title=\"電影2\" src=\"http://www.qq.com:808/asdsadsd+_#$\"/>";
String mode = "<movie\\s*title=([^>]*)src=(?=\"?http|https)([^>]*)/>";
Pattern p = Pattern.compile(mode);
Matcher m = p.matcher(s);
HashMap<String, String> map = new HashMap<String, String>();
while (m.find()) {
System.out.println("find...");
String title = m.group(1);
String url = m.group(2);
System.out.println("title:" + title + ",src:" + url);
map.put(title, url);
}
Ⅱ url在java中如何用正則匹配
這需要用到正則嗎?直接url.contains("bbs") 不就行了:
publicclassTest{
publicstaticvoidmain(String[]args){
Stringurl="http://www.xxx.aaa.cn/bbs/topic/xxx";
System.out.println(url.contains("bbs"));
}
}
一定要正則的話可以這樣:
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassCopyOfTest{
publicstaticvoidmain(String[]args){
String[]urls={
"http://www.xxx.aaa.cn/bbs/topic/xxx",
"http://www.xxx.aaa.com/bbs/topic/xxx",
"http://www.yyy.bbb.cn/aas/xxx/xxx/x"
};
Stringregex="/bbs";
Patternp=Pattern.compile(regex);
for(Stringurl:urls){
Matcherm=p.matcher(url);
if(m.find()){
System.out.println(url);
}
}
}
}
Ⅲ Java中如何用正則表達式解析頁面,提取所有URL
先用正則表達式把所有的url找返態飢出來string reg = @"(?is)<a[^>]*href=(['""]?)(?<url>[^>'""閉猜\s]+)[^>]*>";然後再替換漏返掉你想要的
Ⅳ java 用正則表達式如何獲取網頁background:url(),或background-image:url()括弧裡面的內容
可以用如下正則:
/.*background[^;"]+url\(([^\)]+)\).*/gi
給你兩個例子:
javascript:alert('background-image:url(aa.jpg)'.replace(/.*background[^;"]+url\(([^\)]+)\).*/gi,'$1'));
javascript:alert('background:#FFFFFF url(bb.jpg);color:#FF0000;'.replace(/.*background[^;"]+url\(([^\)]+)\).*/gi,'$1'));
Ⅳ java正則表達式截取URL
Stringreg=".*\/\/([^\/\:]*).*";
Stringstr1="http://192.168.1.6:9999/caservice/webservice/caServiceWs?wsdl";
System.out.println(str1.replaceAll(reg,"$1"));
Stringstr2="http://192.168.1.6/caservice/webservice/caServiceWs?wsdl";
System.out.println(str2.replaceAll(reg,"$1"));
Ⅵ java正則 提取網頁中的連接
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.URL;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassMain
{
publicstaticStringgetHTML(Stringspec,StringcharsetName)
{
try
{
URLurl=newURL(spec);
HttpURLConnectionhuc=(HttpURLConnection)url.openConnection();
InputStreamReaderisr=newInputStreamReader(huc.getInputStream(),charsetName);
BufferedReaderbr=newBufferedReader(isr);
StringBuilderbuilder=newStringBuilder();
Stringline=null;
while(null!=(line=br.readLine()))
{
builder.append(line);
}
br.close();
isr.close();
huc.disconnect();
returnbuilder.toString();
}
catch(Exceptione)
{
e.printStackTrace();
}
return"";
}
publicstaticList<String>陵物局getAttr(Stringhtml,StringnodeName,Stringattr)
{
Stringregex="(?i)<"尺讓+nodeName+"\b[^>]*"+attr+"[="'\s]+([^"']*)["']?[^>]*>";
Patternpattern=Pattern.compile(regex);
Matchermatcher=pattern.matcher(html);
ArrayList<String>list=newArrayList<String>();
while(matcher.find())
{
list.add(matcher.group(1));
}
returnlist;
}
publicstaticvoidmain(String[]args)
{
Stringhtml螞和=getHTML("ht和諧tp://..com/question/562228126305552124.html?entry=qb_ihome_tag","UTF-8");
List<String>list=getAttr(html,"a","href");
for(inti=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
}
}
Ⅶ java 正則表達式提取網頁url
(?<=(href=\")).*?(?=\")
經過測試的,完全可以用
Ⅷ java 提取字元串中包含所有的url, 正則表達式怎麼寫
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
String host = "www.java2s.com";
String file = "/index.html";
String[] schemes = {"http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",
"jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",
"systemresource"};
for (int i = 0; i < schemes.length; i++) {
try {
URL u = new URL(schemes[i], host, file);
System.out.println(schemes[i] + " is supported\r\n");
} catch (Exception ex) {
System.out.println(schemes[i] + " is not supported\r\n");
}
}
}
}