-2

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

  • See [this quick try](https://regex101.com/r/Jtq8ry/1), maybe it helps (not much time at the moment). – bobble bubble Oct 20 '22 at 16:46
  • Thanks, I've tried and it doesn't work for some reason. I'm using the replaceAll method. – Aleksandar Grujic Oct 20 '22 at 18:29
  • [Java demo @tio.run](https://tio.run/##dVJbT8IwFH7nV5wsPGwqSzcxMSPEoKI@KDPskWFSR4Fi1zVd5yXIb8euK1FRTpN@p1/PrT1nhV9xZzV72W5pLgqpYKUJv1KU@ZIsyLv/gFW2JLLXOnD/iJUikvdaGcNlCcN3nAtGYN0CLaJ6ZjSDUmGl4bWgM8gx5W6iJOWLyRSwXJSeNa5lTjlm0FyDSQB9cJ7cNE0d9yKqYfJU79Ojej/x1qcbb5/93CeMu9d2ev/nKRvQiVK9kJZAh06dWBBulBCFqBOgTngKQRh1z3ZkaEgEwVnUPTfkUMpCAi8UzIuKzww3YPoVSU7V0hxHMQzH43j8bT8Qwhxu4kuDV6PkbmC024Z4ZJgToz3E18P73zV1AaEIoSY2eTPY1H@gTL34wa@onktV/0Q7qA3b4R9D228QFvs7xs@KXFBGXNM2b9/PzpHuf4P9XQTfMm7TBq93aBjKitWlWXM9foLhjAwYc03VPxyTj1KR3C8q5QvtrBh3naS2oUpPbsFtsAgcOLa69d60NtvtFw) from regex101 code generator. – bobble bubble Oct 20 '22 at 18:41
  • No idea why generator put `\\\"` [works with `\"` too](https://tio.run/##dVJNT8MwDL3vV1jRDi2wKi1DQp0mNGDAAShaj@uQQhdGRppGacqHYL99pGmm8TXnYPvl@cWKvSQvpLecP6/XrJCl0rA0QFBrxgNFF/QtuCE6f6Jq0Nlxf0e0pkoMOjknVQXjN1JITuGjA8Zk/cBZDpUm2riXks2hIEx4qVZMLKYzIGpR@Y7c2CMThEN7DfYBGAK69zLkncQZmt5naLaXoQP/43Dlb/PPbWiofhcN/lesWmckM3OwsdCIZSiRVNggwhHuhbgXHUIYxf2jDRhZEEN4FPePLThWqlQgSg2PZS3mFhtx029aMP1k09sExpNJMtnyR1La5CI5tf7sNr0a2eiyBe44EdRGN8n5@PpnT33AOMa41aav1rf972jTHLHzK@qHSjc/0Q0bYjf6Q3STBen8cIMEeVlIxqlnB@T/rnMbYybd@uFGIXCI147BH@wae1XzpjVHN4smOcnpiHPPdv2tMH2vNC2CstaBNMWaCw@lDYdps6OlcGIxINh3satedVbr9Rc) :) – bobble bubble Oct 20 '22 at 19:07
  • Hmm, surprisingly but it's not working. See here: https://freeimage.host/i/tiggQn. While the regex I posted above works, it removes only the 4. element, and not the last one too. – Aleksandar Grujic Oct 20 '22 at 20:29
  • 1
    Weird, hope you find why it's not working. I can't see the reason at first glance. Maybe because of the first and last doublequote, last [try this one](https://regex101.com/r/Jtq8ry/2) (also subst changed to `$1\"\"`) – bobble bubble Oct 20 '22 at 20:46
  • Well, @bobblebubble, you just made my day! This is working, thank you! – Aleksandar Grujic Oct 20 '22 at 21:37
  • Welcome @AleksandarGrujic so I put an answer for you :) Glad we got it to work! – bobble bubble Oct 20 '22 at 22:57

1 Answers1

3

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).

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Hi, me again. May I ask if you can help me one more time? It's because I need to shift the last element 3 spaces to the left. So now it's no longer the last element, but I still need to replace it with "". I have now 21 elements, and it is on the 18. – Aleksandar Grujic Oct 21 '22 at 13:44
  • Ok, thanks. I've created a new question. https://stackoverflow.com/questions/74154861/how-to-replace-two-elements-in-a-java-string-using-regex – Aleksandar Grujic Oct 21 '22 at 14:05
  • @AleksandarGrujic I see your new question got closed (just remove it if you like). I understand now just the position of the previous last has changed and everything else seems to be same. Try to search for [this pattern and replace with `$1\"\"$2`](https://regex101.com/r/mxDP1r/2) – bobble bubble Oct 21 '22 at 14:44