Java regex to strip out XML tags, but not tag contents

前端 未结 6 1578
有刺的猬
有刺的猬 2021-02-08 13:37

I have the following Java code:

str = str.replaceAll(\"<.*?>.*?|<.*?/>\", \"\");

This turns a String like so:



        
6条回答
  •  余生分开走
    2021-02-08 13:44

    You can try this too:

    str = str.replaceAll("<.*?>", "");
    

    Please have a look at the below example for better understanding:

    public class StringUtils {
    
        public static void main(String[] args) {
            System.out.println(StringUtils.replaceAll("How now brown cow."));
            System.out.println(StringUtils.replaceAll("How now brown cow."));
        }
    
        public static String replaceAll(String strInput) {
            return strInput.replaceAll("<.*?>", "");
        }
    }
    

    Output:

    How now brown cow.
    How now brown cow.
    

提交回复
热议问题