I actually wrote a regex expression to search for web URLs in a text (full code below) but on running the code, console prints out only the last URL in the text. I don't know what's wrong and I actually used a while loop. See code below and kindly help make corrections. Thanks
import java.util.*;
import java.util.regex.*;
public class Main
{
    static String query = "This is a URL http://facebook.com" 
    + " and this is another, http://twitter.com "
    + "this is the last URL http://instagram.com"
    + " all these URLs should be printed after the code execution";
    public static void main(String args[])
    {
        String pattern = "([\\w \\W]*)((http://)([\\w \\W]+)(.com))";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(query);
        while(m.find())
        {
             System.out.println(m.group(2));
        }
    }
}
On running the above code, only http://instagram.com gets printed to the console output
 
     
     
     
     
    