简体   繁体   English

正则表达式; 如何获得所有比赛?

[英]regular expression; how to get all matches?

I'm looking through the sourcecode of a website and I want to find the ids of all videos on that website: 我正在浏览网站的源代码,并且想要查找该网站上所有视频的ID:

Pattern p = Pattern.compile("(video_meta-[[a-z][0-9]]{32})");
            Matcher m = p.matcher(source_code_string);

            while (m.find()) { 

                    video_id_string = m.group(0);
            }

But i'm only getting the last possible match... How do I get all previous matches? 但是我只得到了最后一场可能的比赛...如何获得之前的所有比赛?

Your while loop is overwriting the video_id_string . 您的while循环正在覆盖video_id_string The value after the loop will be whatever was the last thing that was assigned to it. 循环后的值将是分配给它的最后一件事。

If you want to collect all matches, consider using a List . 如果要收集所有匹配项,请考虑使用List

I'm going to go with the obvious one here. 我将在这里选择显而易见的一个。 You haven't indicated that any code has been removed from your sample, so can we assume that this while loop is complete? 您尚未表明已从示例中删除了任何代码,因此我们可以假定此while循环已完成吗? If so, it will only exit the loop after iterating through all matches, leaving the last match in video_id_string. 如果是这样,它将仅在遍历所有匹配项后退出循环,而将最后一个匹配项保留在video_id_string中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM