I am not soo good in RegEx. Can somebody help me to to replace
<MessageParam name="0" desc="Source Queue" />
with
<MessageParam name="0" desc="Source Queue"></MessageParam>
using regular expression
I am not soo good in RegEx. Can somebody help me to to replace
<MessageParam name="0" desc="Source Queue" />
with
<MessageParam name="0" desc="Source Queue"></MessageParam>
using regular expression
Regex to match:
(<\s*MessageParam[^>]*)/\s*>
Replacement string:
$1></MessageParam>
You may need to escape the \ character (add an extra \ before it).
I assume > does not appear in the value for the attributes, and the XML is valid.
More generalized version:
Regex to match:
<\s*([^\s>]+)([^>]*)/\s*>
Replacement string:
<$1$2></$1>
For this one, I'm not sure of all the assumptions that I have made. But I still assume > does not appear in the value for the attributes, and the XML is valid.
<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1> Replace it with: <$1/> (Thanks to thetopsites.net/article/51312414.shtml)Regex
<(\w+)(.+?)/>
replace with
<$1$2></$1>
public static void format(String xmlNode) {
Pattern patt = Pattern.compile("(<MessageParam[^>]*)(\\s*/>)");
Matcher mattcher = patt.matcher(xmlNode);
while (mattcher.find()){
String result = mattcher.replaceAll("$1></MessageParam>");
System.out.println(result);
}
}
replaceAll in String class will do, not sure why you go through all the trouble of writing Pattern/Matcher loop.xmlString.replaceAll("(<MessageParam[^>]*)(\\s*/>)", "$1></MessageParam>");This worked for me:
xmlString.replaceAll("<(\\w*:*\\w+)/>", "<$1></$1>");