简体   繁体   English

vim 正则表达式 - 在行尾匹配任意数量的空格,除了 2

[英]vim regex - match any number of whitespace at end of line except 2

I want to write a fixer for ale to remove all whitespace at the end of a line except for a double whitespace - in markdown this is used to create a linebreak.我想为 ale 编写一个修复程序,以删除除双空格之外的行尾的所有空格 - 在 markdown 中,这用于创建换行符。

I need to match "at end of row, 1 or more white space AND not 2 white space"我需要匹配“在行尾,1 个或多个空格而不是 2 个空格”

kind of like \s\+$\&\s\{^2}$ except that ^ is not negation inside curly brackets.有点像\s\+$\&\s\{^2}$除了^不是大括号内的否定。 Some googling reveals that negating a count of a meta character seems to be a particularly niche problem.一些谷歌搜索显示,否定元字符的计数似乎是一个特别小众的问题。

You can use您可以使用

:%s/\v(\s)@<!\s(\s{2,})?$//g

Details细节

  • % - search on all lines % - 搜索所有行
  • s - substitute s - 替代品
  • \v - very magic mode \v - 非常神奇的模式
  • (\s)@<! - location not immediately preced with a whitespace - 位置没有立即以空格开头
  • \s - a whitespace \s - 一个空格
  • (\s{2,})? - an optional occurrence of two or more whitespaces - 两个或多个空格的可选出现
  • $ - end of line $ - 行尾
  • g - all occurrences on the line. g - 线上的所有出现。

This is how this regex works (translated into PCRE).这就是这个正则表达式的工作方式(翻译成 PCRE)。

(This really should be a comment but formatting is important, here) (这真的应该是一个评论,但格式很重要,在这里)

How do you want the following snippet to look after you have "fixed" it (spaces marked with _ )?您希望以下代码段在您“修复”它后如何处理(标有_的空格)?

First line, without trailing spaces
Second line, with one trailing space_
Third line, with two trailing spaces__
Fourth line, with more than two trailing spaces______

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM