Vim regex backreference

I want to do this:

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

Why doesn't shop_(*) match anything?


Solution 1:

There's several issues here.

  1. parens in vim regexen are not for capturing -- you need to use \( \) for captures.

  2. * 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 .* it 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.

Solution 2:

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)

Solution 3:

If you would like to avoid having to escape the capture parentheses and make the regex pattern syntax closer to other implementations (e.g. PCRE), add \v (very magic!) at the start of your pattern (see :help \magic for more info):

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