Using regular expressions to dispose of specific characters in the content, the following code removes the attribute width setting in the P tag. The P tag is processed and then stitched into a string
/** * Remove the width style setting for P tags * @param content * @return */public static string Formatcodep (string content) {String regex = "<p [^>]*?>"; Pattern pattern = pattern.compile (regex,pattern.case_insensitive); Matcher m = pattern.matcher (content); StringBuilder builder = new StringBuilder (); int sindex = 0;//is mainly used to identify the starting position of the lookup int eindex = 0;//is mainly used to identify the starting position while (M.find ()) {String data = M.grou P (); Eindex = M.start (); Builder.append (content.substring (Sindex,eindex)); data = Data.replaceall ("width[^;] *?;", ""); Builder.append (data); Sindex = M.end (); } builder.append (Content.substring (Sindex)); return builder.tostring (); public static void Main (string[] args) {String content = "<p style=\" table-layout:fixed; font-size : 14px; Word-break:break-all; line-height:28px; letter-spacing:1px; Width:1175px; Clear:both; Word-wrap:break-word; margin:10px Auto; Color:rgb (51, 51, 51); White-space:normal; Font-family:verdana, Arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); > Recently used a jquery plugin when making a template, when the site navigation is scrolled to the top of the currently visible page, fixed at the top and scrolled with the window, There are a lot of Web site front-page templates that have similar effects. </p> "; String bstring = FORMATCODEP (content); System.out.println (bstring); }
Java uses regular expressions