Reputation: 163
Guys i want to extract src="" out of this text
<div style="margin-bottom:20px;"><img width="750" height="369" src="https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="Hamza Shehbaz" srcset="https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg 750w, https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ-300x148.jpg 300w" sizes="(max-width: 750px) 100vw, 750px" /></div> <p>لاہور: پنجاب اسمبلی میں اپوزیشن لیڈر حمزہ شہبازشریف آج لندن کے لیے اڑان بھریں گے، وہ براستہ دوحا لندن جائیں گے۔ تفصیلات کے مطابق مسلم لیگ ن کے رہنما حمزہ شہبازشریف آج قطرایئرویز کی پرواز 629 کے ذریعہ لندن روانہ ہوں گے ۔ انہوں نے لاہور ہائی کورٹ کو بیرون ملک روانگی سے متعلق آگاہ […]</p> <p>The post <a rel="nofollow" href="https://urdu.arynews.tv/hamza-shehbaz-will-depart-for-london-today/">حمزہ شہباز آج لندن روانہ ہوں گے</a> appeared first on <a rel="nofollow" href="https://urdu.arynews.tv">ARYNews.tv | Urdu - Har Lamha Bakhabar</a>.</p>
can you tell me how i do
Upvotes: 1
Views: 66
Reputation: 18357
You can use regex to extract the value, although since it looks like HTML, you should use some HTML/XML parser to extract the value. Here is a simple code that will get you the value of src attribute,
String s = "<div style=\"margin-bottom:20px;\"><img width=\"750\" height=\"369\" src=\"https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"Hamza Shehbaz\" srcset=\"https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg 750w, https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ-300x148.jpg 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" /></div> <p>لاہور: پنجاب اسمبلی میں اپوزیشن لیڈر حمزہ شہبازشریف آج لندن کے لیے اڑان بھریں گے، وہ براستہ دوحا لندن جائیں گے۔ تفصیلات کے مطابق مسلم لیگ ن کے رہنما حمزہ شہبازشریف آج قطرایئرویز کی پرواز 629 کے ذریعہ لندن روانہ ہوں گے ۔ انہوں نے لاہور ہائی کورٹ کو بیرون ملک روانگی سے متعلق آگاہ […]</p> <p>The post <a rel=\"nofollow\" href=\"https://urdu.arynews.tv/hamza-shehbaz-will-depart-for-london-today/\">حمزہ شہباز آج لندن روانہ ہوں گے</a> appeared first on <a rel=\"nofollow\" href=\"https://urdu.arynews.tv\">ARYNews.tv | Urdu - Har Lamha Bakhabar</a>.</p>";
Pattern p = Pattern.compile("src=\"([^\"]+)");
Matcher m = p.matcher(s);
if(m.find()) {
System.out.println(m.group(1));
} else {
System.out.println("Couldn't find");
}
Output,
https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg
This solution uses this regex src=\"([^\"]+)
where it matches src="
literally and captures all data until it encounters a doublequote where it stops capturing the data.
EDIT: HTML Parser solution
Here's another solution using Jsoup
HTML/XML parser as some people don't like regex to parse HTML,
String s = "<div style=\"margin-bottom:20px;\"><img width=\"750\" height=\"369\" src=\"https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"Hamza Shehbaz\" srcset=\"https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg 750w, https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ-300x148.jpg 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" /></div> <p>لاہور: پنجاب اسمبلی میں اپوزیشن لیڈر حمزہ شہبازشریف آج لندن کے لیے اڑان بھریں گے، وہ براستہ دوحا لندن جائیں گے۔ تفصیلات کے مطابق مسلم لیگ ن کے رہنما حمزہ شہبازشریف آج قطرایئرویز کی پرواز 629 کے ذریعہ لندن روانہ ہوں گے ۔ انہوں نے لاہور ہائی کورٹ کو بیرون ملک روانگی سے متعلق آگاہ […]</p> <p>The post <a rel=\"nofollow\" href=\"https://urdu.arynews.tv/hamza-shehbaz-will-depart-for-london-today/\">حمزہ شہباز آج لندن روانہ ہوں گے</a> appeared first on <a rel=\"nofollow\" href=\"https://urdu.arynews.tv\">ARYNews.tv | Urdu - Har Lamha Bakhabar</a>.</p>";
Document doc = Jsoup.parse(s);
for (Element element : doc.select("img")) {
System.out.println(element.attr("src"));
break;
}
Prints,
https://urdu.arynews.tv/wp-content/uploads/2018/09/HAMZASHEHBAZ.jpg
Make sure to have Jsoup library and following imports in your code,
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
Upvotes: 2