开发者

Invert regexp in vim

There's a few "how do I invert a regexp" questions here on Stack Overflow, but I can't find one for vim (if it does exist, my Google-fu is lacking today).

In essence I want to match all non-printable characters and delete them. I could write a short script, or drop to a shell and use tr or something similar to delete, but a vim solution would be dandy :-)

Vim has the atom \p to match printable characters, however trying to do this :s/[^\p]//g to match the inverse failed and just left me with every 'p' in the fi开发者_如何学Gole. I've seen the (?!xxx) sequence in other questions, and vim seems to not recognize this sequence. I've not found seen an atom for non-printable chars.

In the interim, I'm going to drop to external tools, but if anyone's got any tricks up their sleeve to do this, it'd be welcome :-)

Ta!


Unfortunately you can't put \p in character classes, although that would be a nice feature. However you can use the negative-lookahead feature \@! to build your search:

/\p\@!.

This will first make sure that the . can only match when it is not a \p character.


I'm also a little puzzled why you can't use the \p. But, [:print:] works fine:

:s/[^[:print:]]//g


If you want to filter file with Unicode (only if fileencoding=utf-8) printable characters, you could do this in three steps: mark all printable characters with not used UTF-8 symbol (for example, with nr2char(0xFFFF)), delete all characters, that are not followed by this symbol and, finally, delete this symbol:

%s/\p\@<=/<ffff>/g
%s/[^<ffff>]<ffff>\@!//g
%s/<ffff>//g

Here you must replace <ffff> with the actual character (if you type this, instead of <ffff> type <C-r>=nr2char(0xFFFF)<CR>).

If you are not working with Unicode use the dsummersl's answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜