gpt4 book ai didi

java - 获取包含垃圾数据的字符串的特定部分

转载 作者:行者123 更新时间:2023-12-01 12:38:56 28 4
gpt4 key购买 nike

所以我得到的这些字符串包含很多我不想要的垃圾数据

"http://v20.lscache8.c.youtube.com/videoplayback?id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9"

所以基本上我只想让它在字符串中搜索 videoplayback?id=*然后复制 videoplayback?id= 和 & 之间的内容

271de9756065677e

然后继续遍历字符串并以相同的方式获取签名

那么任何人都可以帮助我了解如何做到这一点的逻辑和示例吗?

最佳答案

由于您的“包含垃圾数据的字符串”实际上是一个 URL,因此您应该使用 URL类(class)看看教程Parsing a URL

import java.net.*;
import java.io.*;

public class ParseURL {
public static void main(String[] args) throws Exception {

String url = "http://v20.lscache8.c.youtube.com/videoplayback?id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9";
URL aURL = new URL(url);

System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
}
}

输出应该是:

protocol = http
authority = v20.lscache8.c.youtube.com:80
host = v20.lscache8.c.youtube.com
port = 80
path = /videoplayback
query = id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9

为了解析查询,请使用 URLEncodedUtils

String url = "http://v20.lscache8.c.youtube.com/videoplayback?id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");

for (NameValuePair param : params) {
System.out.println(param.getName() + "=" + param.getValue());
}

输出应该是:

id=271de9756065677e
itag=17
ip=0.0.0.0
ipbits=0
expire=999999999999999999"
sparams=ip,ipbits,expireip,ipbits,expire,id,itag
signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9

关于java - 获取包含垃圾数据的字符串的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25306085/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com