Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim regex to replace only datetime

Tags:

regex

vim

I want to replace each line by the datetime string inside each.

Input

D­¬ng ThÞ Anh 19/02/1992 TH12B 10.0 5.0
La C«ng TuÊn Anh 30/01/1995 TH12A 8.5 6.5
NguyÔn §øc Anh 14/10/1995 TH12B 10.0 5.5
Ph1m Tïng Anh 10/04/1994 HTTT11 1.0 4.5

Output

19/02/1995
30/06/1995
14/11/1995
10/03/1994

I tried with the following command:

:'<,'>s/[^=\d{2}\/\d{2}\/\d{4}]/ /g

But there's something wrong.

What I have to change to make the command work?

like image 520
Duong Bach Avatar asked Apr 26 '26 00:04

Duong Bach


1 Answers

You cannot simply "negate" the regular expression; for some cases, an inverted pattern can be written, but this is not a general solution.

Instead, you have to match the entire line and capture the text to be kept. In the replacement part of :substitute, just refer to the captured group (\1). This is a common pattern:

%s#.*\<\(\d\+/\d\+/\d\+\)\>.*#\1#

Note: I've used # delimiters here to avoid the escaping of the / occurring in the pattern.

like image 89
Ingo Karkat Avatar answered Apr 29 '26 11:04

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!