-2

I have this string:

String str = "8240d66c-4771-4fae-9631-8a420f9099ca,458,cross_vendor_certificate_will_expire,1565102543758";

I would like to remove the epoch time from the string using regex I've searched the web but I didn't find a suitable solution. This is my code I have so far:

 public void createHashMapWithAlertCSVContent() throws Exception {
        for(String item: lstServer) { //lstServer is a list contains names of the CSV files 
            String[] contentCSVStr= CmdHelper.Remote.File.cat(SERVER,INDENI_INSIGHT_PATH + "/"+item).split("\n");//Function to get CSV contents
            mapServer.put(FileUtil.removeExtension(item), contentCSVStr);//Finally I add each String[] to hashmap key is the csv file name and String[] is the content of each CSV file
        }
        Assert.assertEquals(mapServer.size(), lstServer.size());
        mapServer.remove("job");
    }

example of possible content:

1. 0,TRIAL,8240d66c-4771-4fae-9631-8a420f9099ca,1566345600000,5,1565102549213
2. 8240d66c-4771-4fae-9631-8a420f9099ca,0,1565102673040
3. 8240d66c-4771-4fae-9631-8a420f9099ca,0.0.0.develop,4418,1565102673009

EDIT: regex might be any location in the string and might exit more than once in the string. length of the epoch time string for sure > 10

tupac shakur
  • 658
  • 1
  • 12
  • 29
  • @azurefrog - I forgot to mention that this is an example for a string I might have, the epoch string might be in any location in the string [in the middle, at the beginning or at the end] – tupac shakur Aug 06 '19 at 15:56
  • 4
    Please describe the format of any input strings you expect to get, and give us an idea of the code you intend to use in a [reprex]. A list of 10+ input string examples would be nice too :) Additionally, how do you intend to identify the epoch string? I see 458 and 1565102543758 in the string with regards to comma-separated numbers. – Avi Aug 06 '19 at 15:58
  • Could you elaborate a little? Assuming you're eventually splitting the string to extract the individual elements how would you recognize them if they can appear at any location (at least offset by 1 if epoch time precedes them)? Besides that, as Avi already said: how would you distinguish between a timestamp and any other number? One way could be to expect timestamps to have a certain length (i.e. you don't expect any timestamps from years back) and your other numbers can't be that large, would that be the case under _all_ circumstances? – Thomas Aug 06 '19 at 16:04
  • @Thomas I'll edit my question – tupac shakur Aug 06 '19 at 16:06
  • What have you tried? What did your search turn up, and in which way was it insufficient? You’ve put a nice effort into wording the question, but otherwise it may look like there isn’t really any effort behind it? – Ole V.V. Aug 06 '19 at 16:19
  • First you need to fully understand and define your problem, i.e. epoch time can be **any number**, e.g. 0 is the epoch time for 1970-01-01 00:00:00. Do you really want to remove all numbers? If not, how do you define which numbers are epoch times and which aren't? If the epoch times are all "current", you could choose numbers >= 1500000000000, which means dates after 2017-07-14 02:40:00. But *you* have to decide on such boundary condition, we can't do that for you. And if dates before year 2000 are valid, then your goal is likely impossible. – Andreas Aug 06 '19 at 16:22
  • @Andreas as you suggested only 'current' times are acceptable as you said dates after 2017-07-14 02:40:00 – tupac shakur Aug 06 '19 at 16:25

1 Answers1

2
    String input = "0,TRIAL,8240d66c-4771-4fae-9631-8a420f9099ca,1566345600000,5,1565102549213";
    String output = input.replaceAll("\\d{10,},|,\\d{10,}", "");
    System.out.println(output);

Output:

0,TRIAL,8240d66c-4771-4fae-9631-8a420f9099ca,5

The vertical bar | in the regular expression denotes two options, one with a number and a comma, the other with the comma before the number. This takes into account that the timestamp may be first or last in the string or somewhere between.

\\d denotes a digit and {10,} that there are at least 10 of them with no upper limit. Please consider yourself whether the lower limit should be 10, 13 or some other number of digits.

Corner case: if the string consists of only one or more timestamps, the above replacement will not remove the last one of them since it insists on removing one comma with each timestamp, and a string consisting of only one timestamp will not have a comma in it.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • How do you know that date >= 1970-01-12 13:46:40 is a valid filter? – Andreas Aug 06 '19 at 16:26
  • 1
    The questioner said in a comment that there are at least 10 digits, @Andreas. i assume, like you, that it means after Januar 12, 1970 (not that earlier timestamps are written with leading zeroes). – Ole V.V. Aug 06 '19 at 16:27
  • 2
    With OP's [latest comment](https://stackoverflow.com/questions/57379756/regex-for-epoch-time-in-millisecond-using-java?noredirect=1#comment101245171_57379756) "only 'current' times", I would suggest `\d{13,}`, which means dates >= 2001-09-09 01:46:40. – Andreas Aug 06 '19 at 16:29
  • As well I will only take into consideration dates from current date time and ahead, past dates are not the case I've to handle. – tupac shakur Aug 06 '19 at 16:31
  • 1
    I agree, @Andreas. I trust the questioner to make the last adjustments himself/herself. – Ole V.V. Aug 06 '19 at 16:31