-1

New to Regex.

Can anyone tell me what the java regex replacement code is to replace the following XML

<?xml version="1.0" encoding="iso-8859-1"?><message xmlns="http://www.origoservices.com">   <m_control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.origoservices.com"><control_timestamp>2014-09-30T14:39:05.6402</control_timestamp>........</message>

and make it

<?xml version="1.0" encoding="iso-8859-1"?><message><control_timestamp>2014-09-30T14:39:05.6402</control_timestamp>........</message>

The namespaces may be in differnt postions each time

1

2 Answers 2

0

You need to use a lookahead here.

<message.*?(?=<control_timestamp)

Replace the matched characters with <message>

DEMO

String src = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><message xmlns=\"http://www.origoservices.com\">   <m_control xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://www.origoservices.com\"><control_timestamp>2014-09-30T14:39:05.6402</control_timestamp>........</message>";
String out = src.replaceAll("<message.*?(?=<control_timestamp)", "<message>");
System.out.println(out);

Output:

<?xml version="1.0" encoding="iso-8859-1"?><message><control_timestamp>2014-09-30T14:39:05.6402</control_timestamp>........</message>
0

you can replace "<message.*<control_timestamp>" with "<message><control_timestamp>". then your output is ready.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.