Pattern found in vim search but not in vim search and replace?

There is a search and replace operation that I am trying to do using feedback and regular expressions in vim. Interestingly, it will only recognize the pattern if I do a clean search, but if I do a search and replace it, it will get error E486: the pattern was not found.

I have a bunch of form function calls:

function( Nullable< double >(1.1), map[FOO] ); 

Where FOO is some other variable name on each line. I want to turn it into

 function( othermap[ FOO ], map[FOO] ); 

If i try

 :%s/Null.*\(map[\)\(.*\)\]/othermap[ \2 \], \1\2\]/g 

He gives me a "Not Found Template Error". Even

 :%s/Null.*\(map[\)\(.*\)\]//g 

will not work because it just does not recognize the pattern. But if I try the following command with the same search regex:

 /Null.*\(map[\)\(.*\)\] 

It is highlighted correctly. After that, I can do% s // othermap [\ 2], \ 1 \ 2] to do my replacement. Therefore, I was able to make my replacement in the end, but I can’t let my life understand why the pattern will be recognized in one case and not in another.

+7
source share
1 answer

I can reproduce the result using copy'n'paste from your question in my vim session. The detailed message I receive is:

 E486: Pattern not found: Null.*\(map[\)\(.*\)\]/othermap[ \2 \], \1\2\]/g 

Note that he lost s/ at the beginning.

However, looking rather closely at this, the problem is the unreleased [ :

 s/Null.*\(map[\)\(.*\)\]/othermap[ \2 \], \1\2\]/g ^ |-- here; you need \[ to match the literal 

I do not use % notation; I would automatically write:

 :g/Null.*\(map\[\(.*\)\]\)/s//othermap[\2], \1/g 

This is a little different than capture. Also, there was no need to use a backslash in \] in the replacement string.

However, this command also works for me:

 :%s/Null.*\(map\[\(.*\)\]\)/othermap[\2], \1/g 
+9
source

All Articles