I have a file containing string like this one :
print $hash_xml->{'div'}{'div'}{'div'}[1]...
I want to replace {'div'}{'div'}{'div'}[1] by something else.
So I tried
%s/{'div'}{'div'}{'div'}[1]/by something else/gc
The strings were not found. I though I had to escape the {,},[ and ]
Still string not found.
So I tried to search a single { and it found them.
Then I tried to search {'div'}{'div'}{'div'} and it found it again.
Then {'div'}{'div'}{'div'}[1 was still found.
To find {'div'}{'div'}{'div'}[1]
I had to use %s/{'div'}{'div'}{'div'}[1\]
Why ?
vim 7.3 on Linux
That’s because the
[and]characters are used to build the search pattern.See
:h patternand use the help filepattern.txtto try the following experiment:Searching for the “[9-0]” pattern (without quotes) using
/[0-9]will match every digit from 0 to 9 individually (see:h \[)Now, if you try
/\[0-9]or/[0-9\]you will match the whole pattern: a zero, an hyphen and a nine inside square brackets. That’s because when you escape one of[or]the operator[*]ceases to exist.Using your search pattern,
/{'div'}{'div'}{'div'}[1\]and/{'div'}{'div'}{'div'}\[1]should match the same pattern which is the one you want, while/{'div'}{'div'}{'div'}[1]matches the string{'div'}{'div'}{'div'}1.