Russell Teapot
Russell Teapot

Reputation: 523

Vim - Substitute command and regexp

I have a little JSON files with some entries, here is a section:

"i":{
    "normale":"3c",
    "bold":"4b",
    "doppio":"6c"},
"is":{
    "normale":"2c",
    "bold":"33",
    "doppio":"66"},

I realized I have to add "\u25" in front of all the values, so I tried this command:

:%s:\("\)\(\d\d"\)\|\("\)\(\d\w"\):"\\u25\2

The idea is to search for either "dd" or "dw", and substitute the first double quote with "\u25 while keeping the rest.This is the result:

"i":{
        "normale":"\u25,
        "bold":"\u25,
        "doppio":"\u25},
    "is":{
        "normale":"\u25,
        "bold":"\u2533",
        "doppio":"\u2566"},

If the matching string has only the two digits, the command works fine: the first double quote (the first group) is substituted and the second group is left as it was. However, if the matching string has a digit and a character, it seems to ignore the second group, substituting the whole string. The two patterns are identical, except for \w, so it should work exactly the same. What's happening?

Upvotes: 0

Views: 92

Answers (1)

Adam Liss
Adam Liss

Reputation: 48330

Vim matches \d to digits; you'd need \x to match hex digits.

But it seems you want to replace all occurrences of :" with :"\u25.

Can you use:

:%s/:"/:"\\u25"/

Or, if you want to prepend \u25 to all occurrences of 2 hex digits,

:%s/\x\x/\\u25&/

Upvotes: 1

Related Questions