String Replacement of the case where the regular expression * of the character string search condition contains a line break.

When you have data in XML and you want to erase the data without making it read by XML Object such as DOM, replacement with String was often used. That is the story at that time.

I replaced the tag I wanted to erase as follows.

python


	String xmlStringData = getXMLfile();
	xmlStringData = xmlStringData.replaceAll("<ABB000001.*?</ABB00001>", "");

Sometimes it didn't disappear. This is because, as the investigation proceeded, the data between the tags was a character string, and line feed data was sometimes included in the expected part of the regular expression "*".

In this case, processing was performed using Pattern's DOTALL mode.

python


import java.util.regex.Pattern;

	String xmlStringData = getXMLfile();
	xmlStringData = Pattern.compile("<ABB000001.*?</ABB00001>", Pattern.DOTALL).matcher(xmlStringData).replaceAll("");

I was able to replace it successfully and delete the tag.

Recommended Posts

String Replacement of the case where the regular expression * of the character string search condition contains a line break.
[Java] Cut out a part of the character string with Matcher and regular expression
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
[Delete the first letter of the character string] Ruby