Java substitution contains HTML tags

Source: Internet
Author: User
Tags html tags

Explain, the document content copied from the open source China in the article, because the plagiarism time is too long, has not found the blogger! Temporarily can not explain source, source Bo master See not gas, can contact me!

My blog, the article belongs to the personal collection, in order to solve the urgency of the future!

 PackageQ;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;  Public classHtmltest {Private Static FinalString regex_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";//to define a regular expression for a script    Private Static FinalString Regex_style = "<style[^>]*?>[\\s\\S]*?<\\/style>";//Regular expressions that define a style    Private Static FinalString regex_html = "<[^>]+>";//Regular expressions that define HTML tags    Private Static FinalString regex_space = "\\s*|\t|\r|\n";//Define a space carriage return line break           /**     * @paramHtmlstr *@return* Delete HTML tags*/     Public Staticstring Delhtmltag (String htmlstr) {Pattern p_script=pattern.compile (Regex_script, pattern.case_insensitive); Matcher M_script=P_script.matcher (HTMLSTR); Htmlstr= M_script.replaceall ("");//Filter Script TagsPattern P_style=pattern.compile (Regex_style, pattern.case_insensitive); Matcher M_style=P_style.matcher (HTMLSTR); Htmlstr= M_style.replaceall ("");//Filter Style LabelsPattern p_html=pattern.compile (regex_html, pattern.case_insensitive); Matcher m_html=P_html.matcher (HTMLSTR); Htmlstr= M_html.replaceall ("");//Filter HTML TagsPattern P_space=pattern.compile (Regex_space, pattern.case_insensitive); Matcher M_space=P_space.matcher (HTMLSTR); Htmlstr= M_space.replaceall ("");//Filter Blank Enter label        returnHtmlstr.trim ();//returns a text string    }            Public Staticstring gettextfromhtml (String htmlstr) {Htmlstr=Delhtmltag (HTMLSTR); Htmlstr= Htmlstr.replaceall ("&nbsp;", "" ");//htmlstr = htmlstr.substring (0, Htmlstr.indexof (". ") +1);        returnHtmlstr; }            Public Static voidMain (string[] args) {String str= "<div style= ' text-align:center; ' > regulation of "Four Winds" to remove the disadvantages of descaling <br/><span style= ' font-size:14px; > </span><span style= ' font-size:18px; ' > Company held the party's mass line education practice activities Mobilization Conference </span><br/><title> I am the title </title><desc> I am desc content </desc ></div> ";    SYSTEM.OUT.PRINTLN (gettextfromhtml (str)); }}

Post the result of the final replacement:

Java substitution contains HTML tags

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.