2

I'm having trouble getting figuring out a couple of S&Rs for some very large documents I'm editing.

  1. I'd like to replace all instances of [##dname = KOxxx] where xxx is any three-digit number with Variable KOxxx

I came up with [##dname = KO\(\d\+\)] which correctly finds the text I'd like to replace, but I can't for the life of me figure out how to get the "xxx" to be preserved in the replace. Any ideas?

  1. I'd like to remove the characters > and < around a string. Example: >xxx< where xxx is anywhere from 2-5 characters long.

I had been using the following regex successfully a few months ago, but now it doesn't seem to be working.

%s#>\(\w\{2,5}\)<#\1#g

Any ideas why this might be failing?

Thanks in advance!

3
  • \d\+ is one or more numbers, not three numbers. \d{3} would be three numbers. Here \w\{2,5} you are saying the first curly brace is literal, take off the backslash, \w{2,5} to say 2 to 5 word characters (make it a . if you want any character). I'm not sure how vim's find and replace works I'd presume your grouped value for the xxx would be in $1 or \1. Commented Jun 29, 2015 at 23:02
  • For future reference, it's better to pose multiple questions separately. Commented Jun 29, 2015 at 23:21
  • Thanks Edward, will do! Commented Jun 30, 2015 at 18:52

1 Answer 1

1

This is what you're after :)

:%s/\[##dname = KO\(\d*\)\]/Variable KO\1/g
Sign up to request clarification or add additional context in comments.

2 Comments

\d* is not any three-digit number
yes, s/he can use \d\(3} instead to be more robust

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.