tags:

views:

120

answers:

3

I want to do this:

%s/shop_(*)/shop_\1 wp_\1/

Why doesn't shop_(*) match anything?

+1  A: 

If I understand correctly, you want %s/shop_\(.*\)/shop_\1 wp_\1/

Escape the capturing parenthesis and use .* to match any number of any character.

(Your search is searching for "shop_" followed by any number of opening parentheses followed by a closing parenthesis)

Stephen
+3  A: 

You've made two mistakes:

  • In Vim, by default, you need to backwhack parentheses if you want them to group.
  • You haven't given the star anything to repeat.

This does what you want:

:%s/shop_\(.*\)/shop_\1 wp_\1/
Zack
I've never heard backwhack before, but I instantly knew what you meant . Yay for new words!
Nick Canzoneri
+3  A: 

There's several issues here. The first is that parens in vim regexen are not for capturing -- you need to use \( \) for captures. The second is that * doesn't mean what you think. It means "0 or more of the previous", so your regex means "a string that contains shop_ followed by 0+ ( and then a literal ). You're looking for ., which in regex means "any character". Put together with a star as .* is means "0 or more of any character". You probably want at least one character, so use .\+ (+ means "1 or more of the previous")

Use this: %s/shop_\(.\+\)/shop_\1 wp_\1/. Optionally end it with g after the final slash to replace for all instances on one line rather than just the first.

Daenyth
This is a much better explanation than my answer.
Zack
Just a nit: the existing regex didn't search for a literal `*)`, just a literal `)`.
Stephen
@Stephen: Woops, you're right. Mistyped there.
Daenyth