Summary of Java Regular expressions

Source: Internet
Author: User

Recently used regular expressionsSince I recently made an Android news client, I used regular expressions several times, so I concluded. 1. Use regular expressions to get the image URL of an article content within an RSS resourceBecause in each news browsing the ListView need the news content in the picture, so you need to crawl the article first photo URL address, watercress photo format is:
You need to get the picture ur address in a lot of content, which requires a Java regular expression to write a method
                /** * Extract the src contents of imag in HTML * The returned value is formatted as * http://img6.douban.com/view/presto/large/public/111.jpg */public String getimgstr ( String htmlstr) {string img = "", TMP = ""; Java.util.regex.Pattern p_image;java.util.regex.matcher m_image; String regex_img = "http://[([a-z0-9]|.| /|\\-)]+. [(jpg) | (BMP) | (GIF) | (PNG)] "; /Picture link Address p_image = java.util.regex.Pattern.compile (regex_img, java.util.regex.Pattern.CASE_INSENSITIVE); m_image = P_ Image.matcher (HTMLSTR), while (M_image.find ()) {img = img + "," + M_image.group ();} System.out.println (Img.indexof (",")); if (Img.indexof (",") >= 0) {string[] url=img.substring (1). Split (","); if ( url.length>1) {if (Url[1].indexof ("JPG")!=-1) {return url[1];} Else{return null;}} Else{return null;}} Elsereturn img;}
This method returns the first image URL address, and people will think that if there is no picture in the article to return NULL, you can also be handsome to choose the article without pictures. 2. Filter text HyperlinksPresumably everyone in the website, often with JS with hyperlinks to greet, I encountered the problem is: an article from the RSS to get to, the inside of the text contains a hyperlink, display in the phone font will have color, this will be very difficult to see. The beginning of the idea is to Baidu search: Android WebView to eliminate HTML hyperlinks, get to solve the browser can not use hyperlinks to the way
            Mask Hyperlink    bodyhtml.setwebviewclient (New Webviewclient () {public    Boolean shouldoverrideurlloading (WebView View, String URL) {            return true;        }    });

Click the hyperlink does not jump to other pages, but the font is still color, so I searched the wrong direction, so search Java regular expression filter regular expression, I got the results I need, since Baidu is not so smart as everyone thought, Do it as accurately as possible (the first page of the search results appeared in PHP version, I was drunk)
                Remove hyperlink, retain font string regex= "<\\s*a.*?/a\\s*>"; Title=title.replaceall (Regex, "");
Of course, you can also remove the hyperlink font can be.
Finally the sentence: just stick to find, you will be able to find what you need!!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of Java Regular expressions

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.