Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to remove date and time from all lines

[2022-01-04T11:41:50.866Z] ### Step X
[2022-01-04T11:49:37.836Z] ### Step Y
[Error] Not Found ....
[couchbaseUtils] Not Found...
[2022-01-04T19:00:59.833Z] [INFO] [2022]

I want to remove all the dates [2022-01-04T11:41:50.866Z] using Regex.

What would be the right pattern for this? I would like to learn. Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I don’t know what language you are using. But here is a very simple regex that should work on almost all languages.

\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\]

It’s a very simple regex that exactly matches your date and time format. If you still need explanation, I think the one provided on the top-right in the link I posted should be sufficient.

If you are using java, you can do it the following way.

input = input.replaceAll("\\[\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z\\]", "");

It finds the string in the given regex pattern and replaces it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading