1

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?

ROMANIA_engineer
  • 51,252
  • 26
  • 196
  • 186
Faiz Ali
  • 131
  • 1
  • 10
  • 1
    ... parse, find indexes and then call `String.substring(...)`? What code have you tried? – Jashaszun Jun 25 '15 at 23:09
  • Your text looks like XML. Why don't you use XML parser and remove nodes or their content you don't want? Also [regex is not best tool to parse XML](http://stackoverflow.com/q/701166/1393766). – Pshemo Jun 25 '15 at 23:24

2 Answers2

1

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>");
Federico Piazza
  • 28,830
  • 12
  • 78
  • 116
0
S = S.replaceAll("<name>timestamp</name>[^<]*<value>[^<]*</value>", "");
jmrah
  • 4,715
  • 2
  • 25
  • 34