简体   繁体   中英

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:

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 . 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 .

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? If so, it will only exit the loop after iterating through all matches, leaving the last match in video_id_string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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