导航:首页 > 编程语言 > 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相关的资料

热点内容
iphone13对wap3加密 浏览:553
pdf文件打开失败 浏览:911
dubbo怎么调用不同服务器接口 浏览:38
全能解压王app历史版本 浏览:73
优先队列与拓扑排序算法 浏览:279
pdf转换formacbook 浏览:869
pdf文件内容怎么编辑 浏览:46
134压缩机排气温度多少 浏览:254
unity等待编译后 浏览:804
黑鲨手机锁屏视频在哪个文件夹 浏览:779
wow地图解压后怎么压缩 浏览:819
有pdf却打不开 浏览:460
七星彩软件app怎么下载 浏览:217
32单片机的重映射哪里改 浏览:816
为什么前端不用刷算法题 浏览:708
对称加密系统和公钥加密系统 浏览:428
历史地理pdf 浏览:606
物联网云服务器框架 浏览:648
sybaseisql命令 浏览:183
android权威编程指南pdf 浏览:663