vessel
vessel

Reputation: 29

Vim regex captures part of group pattern at the end of the string

I'm fairly new to Vim and regex. I have a file that looks something like this :

parent = 0001,,,lje1 
parent 0001 = category = 0002,,,ljef
parent 0001 = category = 0003,,,ljex4
parent = 0004,,,lxen 
parent 0004 = category = 0005,,,lxvr

I am looking to get the last set of numbers, commas and the code at the end of each line . If I use the following regex:

/[0-9]*,*l[a-z0-9]*$/

It matches the correct part on each line... numbers, comma and code. But if I try to do a replacement as such:

%s/.*\([0-9]*,*l[a-z0-9]*$\)/\1

I am just left with something that looks like this:

lje1 
ljef
ljex4
lxen 
lxvr

What I would like to be left with is this:

0001,,,lje1 
0002,,,ljef
0003,,,ljex4
0004,,,lxen 
0005,,,lxvr

If anyone has any idea of what I'm doing wrong, please help. I have searched and checked other posts but I can't seem to find a solution.

Cheers

Upvotes: 1

Views: 1334

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

Just replace the all the chars upto the last space character with an empty string.

/.* /

DEMO

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

You need lazy matching with .\{-} instead of .*:

%s/.\{-}\([0-9]*,*l[a-z0-9]*$\)/\1

Upvotes: 2

Related Questions