Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON格式URL列表的Java正则表达式模式_Java_Android_Regex - Fatal编程技术网

JSON格式URL列表的Java正则表达式模式

JSON格式URL列表的Java正则表达式模式,java,android,regex,Java,Android,Regex,我很难找到我需要匹配的正则表达式 源代码如下所示: \"url240\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.240.mp4?extra=hash\" ,\"url360\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.360.mp4?extra=hash\" ,\"url480\":\"https:\\\/\\\/domain.com\\\/id1

我很难找到我需要匹配的正则表达式

源代码如下所示:

\"url240\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.240.mp4?extra=hash\"
,\"url360\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.360.mp4?extra=hash\"
,\"url480\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.480.mp4?extra=hash\"
,\"url720\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.720.mp4?extra=hash\"
我需要匹配所有URL(可以是一个、两个、三个或全部四个,这取决于提供给应用程序的源代码),并将它们存储在ArrayList中

这是我通常使用的代码:

List<String> sourceList = new ArrayList<String>();
Pattern vPattern = Pattern.compile(?);
Matcher videoMatcher = vPattern.matcher(source);
while (videoMatcher.find())
    sourceList.add(videoMatcher.group(2));

但是它不起作用。

对于您列出的源代码,您的表达式是正确的。这个问题与字符串转义有关

在Java中使用表达式时,我们在使用它之前会对字符串进行两次解析,第一次是在创建Java字符串时,第二次是在正则表达式引擎中,您要匹配的是
\“url[0-9]+\:\”(*?\”
,您已经完成了第一级转义(对于正则表达式引擎)。接下来,我们需要再次为Java转义字符串

因此,为了获得您发布的表达式,我们需要转义所有的
\
,以便它们成为最后一个字符串中的
\
,而不是由Java作为转义序列处理,并且我们需要转义
,因为整个过程是一个字符串,而未转义的
终止字符串

这意味着我们需要在所有的
\
前面加上
\
”,只要我们在
之前用
\
替换
\
”,
替换
”,
,这可以很容易地通过在任何编辑器中使用搜索和替换来完成,只要我们在
之前用
\
替换
”,
替换
,因为我们不想在退出
时添加的
\

这就给了我们:
“\url\\d+\”:(.*)。

除此之外,表达式只有一个匹配组,因此我们需要得到
videoMatcher.group(1)
not
videoMatcher.group(2)

测试代码:

public static void main(String[] args) throws Exception {
    String source = "\\\"url240\\\":\\\"https:\\\\\\/\\\\\\/domain.com\\\\\\/id123456\\\\\\/files\\\\\\/video.240.mp4?extra=hash\\\",\n\\\"url360\\\":\\\"https:\\\\\\/\\\\\\/domain.com\\\\\\/id123456\\\\\\/files\\\\\\/video.360.mp4?extra=hash\\\",\n\\\"url480\\\":\\\"https:\\\\\\/\\\\\\/domain.com\\\\\\/id123456\\\\\\/files\\\\\\/video.480.mp4?extra=hash\\\",\n\\\"url720\\\":\\\"https:\\\\\\/\\\\\\/domain.com\\\\\\/id123456\\\\\\/files\\\\\\/video.720.mp4?extra=hash\\\"";
    String pattern = "\\\\\"url\\d+\\\\\":\\\\\"(.*?)\\\\\"";

    System.out.println("Source: " + source);
    System.out.println("\nPattern: " + pattern);

    List<String> sourceList = new ArrayList<String>();
    Pattern vPattern = Pattern.compile(pattern);
    Matcher videoMatcher = vPattern.matcher(source);
    while (videoMatcher.find()) {
        sourceList.add(videoMatcher.group(1));
    }

    System.out.println("\nResult:" + Arrays.toString(sourceList.toArray(new String[0])));
}
Source: \"url240\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.240.mp4?extra=hash\",
\"url360\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.360.mp4?extra=hash\",
\"url480\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.480.mp4?extra=hash\",
\"url720\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.720.mp4?extra=hash\"

Pattern: \\"url\d+\\":\\"(.*?)\\"

Result:[https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.240.mp4?extra=hash, https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.360.mp4?extra=hash, https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.480.mp4?extra=hash,     https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.720.mp4?extra=hash]

正如我们所看到的,模式字符串的值,一旦被取消转义一次,就是我们想要的正则表达式。

谢谢!,我昨天算出来了,但你的答案是正确的。
Source: \"url240\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.240.mp4?extra=hash\",
\"url360\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.360.mp4?extra=hash\",
\"url480\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.480.mp4?extra=hash\",
\"url720\":\"https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.720.mp4?extra=hash\"

Pattern: \\"url\d+\\":\\"(.*?)\\"

Result:[https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.240.mp4?extra=hash, https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.360.mp4?extra=hash, https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.480.mp4?extra=hash,     https:\\\/\\\/domain.com\\\/id123456\\\/files\\\/video.720.mp4?extra=hash]