mercredi 25 novembre 2020

Pattern and Matcher is not giving the result what it should give in java

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
    public static void main(String[] args) {
        ArrayList<String> celebURLs = new ArrayList<String>();
        ArrayList<String> celebName = new ArrayList<String>();
        String webHtml = "<img loading=\"lazy\" width=\"346\" height=\"440\" src=\"https://svenskainfluencers.nu/wp-content/uploads/Skärmavbild-2020-06-28-kl.-22.31.16.png\" alt class=\"wp-image-230\" srcset=\"https://svenskainfluencers.nu/wp-content/uploads/Skärmavbild-2020-06-28-kl.-22.31.16.png 346w, https://svenskainfluencers.nu/wp-content/uploads/Skärmavbild-2020-06-28-kl.-22.31.16-236x300.png 236w\" sizes=\"(max-width: 346px) 100vw, 346px\"><figcaption>Instagram: Isabella Löwengrip</figcaption>";
        Pattern p = Pattern.compile("<figcaption>Instagram: (.*?)</figcaption>");
        Matcher m = p.matcher(webHtml);
        while (m.find()){
            celebURLs.add(m.group(1));
            System.out.println(m.group(1));
        }
        Pattern pp = Pattern.compile("<img loading=\"lazy\" width=\"346\" height=\"440\" src=\"(.*?) alt=\"\" class=");
        Matcher mm = pp.matcher(webHtml);
        while (mm.find()){
            celebName.add(mm.group(1));
            System.out.println(mm.group(1));
        }
    }

In the above code I am practicing to make an android app name Guess the Celebrity that will download webcontent in html form form website and then i will use a random image to show and 4 different names.

To make it easier i have taken the img tag of one of the Celebrity and figcaption tag in which celebrity name is given and stored it in a String webHtml.

The code is running but it only gives me celebrity name but it does not give me the img url that i want to download.

Can you please help me to figure out how to resolve it.

Aucun commentaire:

Enregistrer un commentaire