Regex: Select and Delete all content of file, except “DATE” format

Deadly 提交于 2021-02-16 21:49:57

问题


I have a file with many dates in the format of 03/07/2017.

For exemple:

My lovely letter can't be read...

academicsdirectbu.tps 10.7 k 03/07/2017 11:39 -a--

bash: remove all files except last version in file name 02/04/2015 before Table

So, I want to match and delete all content except Date format. First I made a regex to find all Dates:

\d{1,2}/\d{1,4}/\d{1,4}

And I include this into another regex \b(?!(\d{1,2}/\d{1,4}/\d{1,4}))[\w-]+\b

But, my second regex, which has to select and Delete all content of file, except "DATE", doesn't work too good.

Can anyone help me a little bit? So, my desire output should be:

03/07/2017

02/04/2015


回答1:


You may use

Find What: (\d{1,2}/\d{1,4}/\d{1,4})|.
Replace With: (?{1}$1\n:) . matches newline: On

Note: You may also add word boundaries around the date pattern to make it a bit more precise, \b(\d{1,2}/\d{1,4}/\d{1,4})\b|..

Now, (\d{1,2}/\d{1,4}/\d{1,4}) will match and capture the date into Group 1 and if not found at the current location, . will match any char there. In the replacement pattern, the date is replaced with itself and a newline is added after it, and if Group 1 is not matched, the char is just removed.



来源:https://stackoverflow.com/questions/43518048/regex-select-and-delete-all-content-of-file-except-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!