1

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

3
  • just a comment, you probably better off replacing * with + Commented Jul 23, 2015 at 8:42
  • Any reason in particular, Im going to go read up on this now.. Commented Jul 23, 2015 at 8:43
  • 1
    your regex will also match letter l by itself. * mean 0 to many where + means 1 to many. depending on your need, you can choose one. Commented Jul 23, 2015 at 8:58

2 Answers 2

4

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

/.* /

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

Good shout, I solved it with the answer below but thanks, didnt even think of this!
2

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

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

2 Comments

Damn.. so simple.. Thanks so much! will accept this answer as soon as it lets me!
Thank you, much appreciated! I have only just been allowed to upvote answers as of this very moment, so there you go, you get my first haha!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.