java - Replace a substring with another string in dynamic content -
i want remove particular text html content. using replaceall method in java replace content "" achieve that.
my content
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr-ca" xml:lang="fr-ca"> or <html xmlns="http://www.w3.org/1999/xhtml" lang="en-au" xml:lang="en-au"> or <html xmlns="http://www.w3.org/1999/xhtml" lang="en-gb" xml:lang="en-gb"> or <html xmlns="http://www.w3.org/1999/xhtml" lang="en-ie" xml:lang="en-ie"> or <html xmlns="http://www.w3.org/1999/xhtml" lang="es-pr" xml:lang="es-pr> or <html xmlns="http://www.w3.org/1999/xhtml" lang="en-us" xml:lang="en-us">
i want remove lang="-"
xml:lang="-"
can see, value of lang , xml:lang changing dynamically. want regular expression can detect particular string sequence replace "" using replaceall(regex, string)
method in java.
this answer based on assumption that
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr-ca" xml:lang="fr-ca"> or <html xmlns="http://www.w3.org/1999/xhtml" lang="en-au" xml:lang="en-au"> or ...
means have html structures
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr-ca" xml:lang="fr-ca"> ... </html>
or
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-au" xml:lang="en-au"> ... </html>
in case instead of regex use html/xml parser jsoup. code like
string htmltext = "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"fr-ca\" xml:lang=\"fr-ca\">" + " <body>hello</body>" + "</html>"; //use xml parser if don't want jsoup change optimize html code document doc = jsoup.parse(htmltext,"",parser.xmlparser()); elements htmltag = doc.select("html"); htmltag.removeattr("lang").removeattr("xml:lang");//remove these attributes selected tag string replaced = doc.tostring(); system.out.println(replaced);
Comments
Post a Comment