導航:首頁 > 編程語言 > java正則提取url

java正則提取url

發布時間:2023-03-23 10:22:21

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");
}
}
}
}

閱讀全文

與java正則提取url相關的資料

熱點內容
蘋果8p手機加密 瀏覽:747
ipad建文件夾怎麼弄 瀏覽:833
iphone13對wap3加密 瀏覽:555
pdf文件打開失敗 瀏覽:913
dubbo怎麼調用不同伺服器介面 瀏覽:40
全能解壓王app歷史版本 瀏覽:75
優先隊列與拓撲排序演算法 瀏覽:281
pdf轉換formacbook 瀏覽:871
pdf文件內容怎麼編輯 瀏覽:48
134壓縮機排氣溫度多少 瀏覽:256
unity等待編譯後 瀏覽:806
黑鯊手機鎖屏視頻在哪個文件夾 瀏覽:781
wow地圖解壓後怎麼壓縮 瀏覽:821
有pdf卻打不開 瀏覽:460
七星彩軟體app怎麼下載 瀏覽:217
32單片機的重映射哪裡改 瀏覽:816
為什麼前端不用刷演算法題 瀏覽:708
對稱加密系統和公鑰加密系統 瀏覽:428
歷史地理pdf 瀏覽:606
物聯網雲伺服器框架 瀏覽:648