Remove html tags using java Regular Expressions

Source: Internet
Author: User

When we use ckeditor or some other online text editors, there are a lot of labels in the content:

A journey of life, a journey of scenery, a journey of prosperity. Open the window of the soul, watching the time and the flowers bloom, watching the mountains are mountains, watching the water is the realm of life will be happy, the most beautiful life, it is the transparency and joy from the depths of the soul.

-- Question

Like in the afternoon of the snow, Ying Yi Jing, book a touch of feelings, see the snow falls into the deepest place of the red dust. Leave the world for now, empty your mind, and try to appreciate the beauty and purity. In the journey of life, there will be unexpected joys and sorrows, and the ground will be red and warm; a tree may bloom, and the snow may be like dust, and the helplessness of the world can be wiped out, it can also purify the soul.

Contains html tags such as images, paragraphs, and line breaks. to calculate the number of words, remove these tags. Use the following java Regular Expression Code:
/*** Delete the Html Tag ** @ param inputString * @ return */public static String htmlRemoveTag (String inputString) {if (inputString = null) return null; string htmlStr = inputString; // String textStr = ""; java. util. regex. pattern p_script; java. util. regex. matcher m_script; java. util. regex. pattern p_style; java. util. regex. matcher m_style; java. util. regex. pattern p_html; java. util. regex. matcher m_html; try {// Define the regular expression {or <script [^>] *?> [\ S \ S] *? <\\/ Script> String regEx_script = "<[\ s] *? Script [^>] *?> [\ S \ S] *? <[\ S] *? \/[\ S] *? Script [\ s] *?> "; // Define the regular expression of the style {or
 
  
] *?> [\ S \ S] *? <\\/ Style> String regEx_style = "<[\ s] *? Style [^>] *?> [\ S \ S] *? <[\ S] *? \/[\ S] *? Style [\ s] *?> "; String regEx_html =" <[^>] +> "; // defines the regular expression p_script = Pattern for HTML tags. compile (regEx_script, Pattern. CASE_INSENSITIVE); m_script = p_script.matcher (htmlStr); htmlStr = m_script.replaceAll (""); // filter the script tag p_style = Pattern. compile (regEx_style, Pattern. CASE_INSENSITIVE); m_style = p_style.matcher (htmlStr); htmlStr = m_style.replaceAll (""); // filter the style tag p_html = Pattern. compile (regEx_html, Pattern. CASE_INSENSITIVE); m_html = p_html.matcher (htmlStr); htmlStr = m_html.replaceAll (""); // filter html Tag textStr = htmlStr;} catch (Exception e) {e. printStackTrace ();} return textStr; // return a text string}
 


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.