Faiz Ali
Faiz Ali

Reputation: 141

Replacing Substring in Java

I have situation in Java where I am reading contents of file in String. It is something like this -

String S = "<name>source</name> <value>NB_System</value> </nameValue> <nameValue> <name>timestamp</name> <value>2015-6-25 22:39:41:455</value> </nameValue> <nameValue> <name>TTL</name> <value>0</value> </nameValue>"

I want to delete the timestamp from the string - timestamp</name> <value>2015-6-25 22:39:41:455</value>

Timestamp is creating issues in comparing results with master copy. How to get rid of timestamp here?

Upvotes: 1

Views: 111

Answers (2)

Federico Piazza
Federico Piazza

Reputation: 30995

If you want to get rid of the tag timestamp with its value, you can use a code like this:

S = S.replaceAll("<name>timestamp.*?<\/value>", "");

On the other hand, if you just want to get rid of the value tag for the timestamp you could use:

S = S.replaceAll("<name>timestamp.*?<\/value>", "<name>timestamp</name>");

Upvotes: 1

jmrah
jmrah

Reputation: 6222

S = S.replaceAll("<name>timestamp</name>[^<]*<value>[^<]*</value>", "");

Upvotes: 0

Related Questions