Java Regular Expressions extract attributes from tags (src connection address), etc.

Source: Internet
Author: User

public class Test {

public static void Main (string[] args) {
String Source = "<p> Sdfasdfasdfsadfasdfasdfasdfasdfasdf</p>System.out.println (Getimgstr (source));
String htmlstr = "<p><video controls=\" Controls\ "durationtime=\" 72\ "filesize=\" 27117469\ "height=\" 200px\ " Poster=\ "https://xxxxxx/b439b0281450abce7f13b2920da04346.png\" src=\ "https://xxxxxxxxxx/ B439b0281450abce7f13b2920da04346.mp4\ "style=\" \ ">&nbsp;</video></p>";

System.out.println (Getvideostr (HTMLSTR));
}


public static set<string> Getimgstr (String htmlstr) {
Set<string> pics = new hashset<> ();
String img = "";
Pattern P_image;
Matcher M_image;
String regex_img = "]*?> ";
P_image = Pattern.compile (regex_img, pattern.case_insensitive);
M_image = P_image.matcher (HTMLSTR);
while (M_image.find ()) {
Get Data
img = M_image.group ();
Match src data in
Matcher m = pattern.compile ("src\\s*=\\s*\"? *?) (\ "|>|\\s+)"). Matcher (IMG);
while (M.find ()) {
Pics.add (M.group (1));
}
}

return pics;
}

public static map<string, string> getvideostr (String htmlstr) {
map<string, string> pics = new hashmap<string, string> ();
String regex_video= "<video.*poster\\s*=\\s* (. *?) [^>]*?src\\s*=\\s* (. *?) [^>]*?> ";
Pattern p = pattern.compile (regex_video,pattern.case_insensitive);
Matcher m = P.matcher (HTMLSTR);
String video= "";
map<string, string> map = new hashmap<string, string> ();
while (M.find ()) {
Video=m.group ();
Matcher Mposter = Pattern.compile ("poster\\s*=\\s*\"? *?) (\ "|>|\\s+)"). Matcher (video);
Matcher MSRC = Pattern.compile ("src\\s*=\\s*\"? *?) (\ "|>|\\s+)"). Matcher (video);
String poster = "";
String src= "";
while (Mposter.find ()) {
Poster=mposter.group (1);
}
while (Msrc.find ()) {
Src=msrc.group (1);
}
Map.put ("poster", poster);
Map.put ("src", SRC);
}
return map;
}
}

Java Regular Expressions extract attributes from tags (src connection address), etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.