Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am having a problem with a regular expression on vim. I have a pattern as follows: http:\/\/\(\w\+\.\?\)\+ [matches http://(AS MANY WORDS FOLLOWED BY DOT OR NOT ENCOUNTERS) e.g. http://wd1.wd2.com]

I have a text as follows:

http://wd1.wd2.com/wd3

I am trying to make this substitution on it:

s/\(http:\/\/\)\(\w\+\.\?\)\+/\1wd4.wd5.com

and the result is http://wd4.wd5.com /wd3 (Notice the white space inserted at the end of the replacement)

How can I avoid having this inserted space? I am afraid is a bug in the regexp engine but I am not sure.

share|improve this question

1 Answer 1

I can't reproduce this. The regex is still not correct however, it must be \w\+.

Using

:s/\(http:\/\/\)\(\w\+\.\?\)\+/\1wd4.wd5.com

results in the following for me.

http://wd4.wd5.com/wd3

Tip: a much cleaner version of your regex can be achieved by using vim's 'very magic' regex mode (c.f. :help regex)

:s#\v(http://)(\w+\.?)+#\1wd4.wd5.com
share|improve this answer
    
Sorry you are right \w\+ is as in the beggining of the question, I have just fixed it in the post –  poissonbreaker Oct 14 '12 at 8:45
    
I am afraid is exactly the same case. Maybe is a failure in the version of VIM I am using. Thanks for your answer. I can not give you any points yet since I have no reputation for doing it, sorry. –  poissonbreaker Oct 14 '12 at 8:52
    
I'm using vim 7.3 mit patches 1-661 (check vim --version) –  buergi Oct 14 '12 at 9:09
    
Vi Improved 7.2 –  poissonbreaker Oct 14 '12 at 10:00
    
Great. Your IMproved version worked like a charm. I hadn't heard of that piece of magic. Excellent. I guess the problem is with the VIm version, because in yours the first way worked, but still is strange that the second had worked for me also. Anyway thanks for your tips, were very helpfull. –  poissonbreaker Oct 14 '12 at 10:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.