使用 java 以毫秒为单位的纪元时间的正则表达式
Regex for epoch time in millisecond using java
我有这个字符串:
String str = "8240d66c-4771-4fae-9631-8a420f9099ca,458,cross_vendor_certificate_will_expire,1565102543758";
我想使用正则表达式从字符串中删除纪元时间我在网上搜索过,但没有找到合适的解决方案。
这是我到目前为止的代码:
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");
}
可能的内容示例:
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
编辑:
正则表达式可能是字符串中的任何位置,并且可能在字符串中多次出现。
纪元时间字符串的长度肯定 > 10
String input = "0,TRIAL,8240d66c-4771-4fae-9631-8a420f9099ca,1566345600000,5,1565102549213";
String output = input.replaceAll("\d{10,},|,\d{10,}", "");
System.out.println(output);
输出:
0,TRIAL,8240d66c-4771-4fae-9631-8a420f9099ca,5
正则表达式中的竖线|
表示两个选项,一个是数字加逗号,另一个是数字前加逗号。这考虑到时间戳可能是字符串中的第一个或最后一个或介于两者之间。
\d
表示一个数字,{10,}
表示至少有10个,没有上限。请自行考虑下限是否应为 10、13 或其他位数。
极端情况:如果字符串由 仅 一个或多个时间戳组成,上述替换将不会删除其中的最后一个,因为它坚持在每个时间戳中删除一个逗号, 并且只有一个时间戳的字符串不会有逗号。
我有这个字符串:
String str = "8240d66c-4771-4fae-9631-8a420f9099ca,458,cross_vendor_certificate_will_expire,1565102543758";
我想使用正则表达式从字符串中删除纪元时间我在网上搜索过,但没有找到合适的解决方案。 这是我到目前为止的代码:
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");
}
可能的内容示例:
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
编辑: 正则表达式可能是字符串中的任何位置,并且可能在字符串中多次出现。 纪元时间字符串的长度肯定 > 10
String input = "0,TRIAL,8240d66c-4771-4fae-9631-8a420f9099ca,1566345600000,5,1565102549213";
String output = input.replaceAll("\d{10,},|,\d{10,}", "");
System.out.println(output);
输出:
0,TRIAL,8240d66c-4771-4fae-9631-8a420f9099ca,5
正则表达式中的竖线|
表示两个选项,一个是数字加逗号,另一个是数字前加逗号。这考虑到时间戳可能是字符串中的第一个或最后一个或介于两者之间。
\d
表示一个数字,{10,}
表示至少有10个,没有上限。请自行考虑下限是否应为 10、13 或其他位数。
极端情况:如果字符串由 仅 一个或多个时间戳组成,上述替换将不会删除其中的最后一个,因为它坚持在每个时间戳中删除一个逗号, 并且只有一个时间戳的字符串不会有逗号。