I have the following String
""00001","Open","2020-10-23 12:45","2022-10-20 15:48","Error not found","Alex Smith","NO ERROR","ErrorApp","FOB","CNSHA","GB","Plane","MODEL","2020-10-24 00:00","New","1","","2022-10-20 15:48""
I need to replace the 4th and the last words (dates) with an empty String "".
I have tried several regex expressions but couldn't get them to work.
The result should be:
""00001","Open","2020-10-23 12:45","","Error not found","Alex Smith","NO ERROR","ErrorApp","FOB","CNSHA","GB","Plane","MODEL","2020-10-24 00:00","New","1","","""
This regex:
actualLines[1].replaceAll("^(\".+?\",\".+?\",\".+?\",\")([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2})(.*)$", "$1$3}");
It replaces the 4th, but I need both 4th and the last element to be replaced.
Does anyone know how this can be done? Thanks
An idea to use an alternation for matching both, the fourth and last double-quoted part:
^((?:\"[^\"]*\",){3})\"[^\"]*\"|\"[^\"]*\"$
See this demo at regex101 or a Java demo at tio.run
Replace with $1\"\"
where $1
is a reference to what's captured by the first group. It holds the first three comma separated quoted parts (?:"[^"]*",){3}
where [^"]
matches characters other than quotes. On the right side of the alternation the part at the $
end gets matched (and $1
is empty).