简体   繁体   中英

Regular expressions in java

String s= "(See <a href=\"/wiki/Grass_fed_beef\" title=\"Grass fed beef\" " +
          "class=\"mw-redirect\">grass fed beef.) They have been used for " +
          "<a href=\"/wiki/Paper\" title=\"Paper\">paper-making since " +
          "2400 BC or before.";

In the string above I have inter-mixed html with text.

Well the requirement is that the output looks like:-

They have been used for paper-making since 2400 BC or before.

Could some one help me with a generic regular expression that would produce the desired output from the given input?

Thanks in advance!

The following expression:

\([^)]*?\)|<[a-zA-Z/][^>]*?>

will match anything that looks like an HTML tag and any parenthesized text. Replace said text with "", and there ya go.

Note: If you try to match any string that has script tags in it, or "HTML" where the author didn't bother to escape < and > when they weren't used as tag delimiters), or a ( without a ), things will probably not work as you'd hoped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM