> When I execute this line
> 
> :%s/\<[01]\+\>/\=Bin2Hex(submatch(0))/
> 
> on this binary tabel
> 
> 0000
> 0001
> [...]
> 1111
> 
> it is all converted nicely to hexadecimal
> 
> However, my binary numbers are prefixed with a % character.
> 
> How to I change the substitute command above so that it works again?
> I tried
> :%s/\<%[01]\+\>/\=Bin2Hex(submatch(1))/
> But this one gives an pattern not found error
> And
> :%s/%\<[01]\+\>/\=Bin2Hex(submatch(1))/
> changes everything into zero.
> So I assume the submatch position is wrong

The \< won't match on the left of a "%" (unless the "%" is in 
your 'isk' setting:  ":help 'isk' for more on that").  Move your 
"%" to the other side of the "\<" and it should work. 
Alternatively, since you've specified the context as requiring a 
"%", you can just skip the "\<" altogether.

That takes care of the pattern-not-found problem.  However, then 
the results of submatch(1) in the replacement now hold "%0001" or 
whatever.  Vim tries to run this through your Bin2Hex, but 
"%0001" isn't a a valid number for parsing, so it chokes.  I 
noticed you also shifted from submatch(0) [the whole match] to 
submatch(1) [something tagged with \(...\) for later 
backreferencing].  The final solution could look something like

   :%s/%\([01]\+\)\>/\=Bin2Hex(submatch(1))

which captures the stuff after the "%" as submatch(1) but 
replaces the whole thing with the output of Bin2Hex().

Hope this helps shed some light on what's going on, as well as 
provide you with a solution.

-tim



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to