正则表达式在vim中插入空格 [英] Regex to insert space in vim

查看:32
本文介绍了正则表达式在vim中插入空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个正则表达式超级菜鸟(只是在阅读我关于它们的第一篇文章),同时努力更好地使用 vim.我想使用正则表达式来搜索冒号 : 的所有实例,这些实例后面没有空格和 insert 一个空格之间冒号及其后的任何字符.

I am a regex supernoob (just reading my first articles about them), and at the same time working towards stronger use of vim. I would like to use a regex to search for all instances of a colon : that are not followed by a space and insert one space between those colons and any character after them.

如果我开始:

foo:bar

我想以

foo: bar

我已经达到了 %s/:[a-z] 但现在我不知道 %s 语句的下一部分要做什么.

I got as far as %s/:[a-z] but now I don't know what do for the next part of the %s statement.

另外,如何更改 :[a-z] 语句以确保它捕获任何不是空格的内容?

Also, how do I change the :[a-z] statement to make sure it catches anything that is not a space?

推荐答案

:%s/:\(\S\)/:\1/g

\S 匹配任何非空白字符,但您需要记住非空白字符是什么.这就是 \(\) 的作用.然后您可以在替换中使用 \1 引用它.

\S matches any character that is not whitespace, but you need to remember what that non-whitespace character is. This is what the \(\) does. You can then refer to it using \1 in the replacement.

所以你匹配一个 :、一些非空白字符,然后用一个 :、一个空格和捕获的字符替换它.

So you match a :, some non-whitespace character and then replace it with a :, a space, and the captured character.

将其更改为仅在只有一个 : 时才修改文本是相当简单的.正如其他人所建议的,使用一些零宽度断言会很有用.

Changing this to only modify the text when there's only one : is fairly straight forward. As others have suggested, using some of the zero-width assertions will be useful.

:%s/:\@!<:[^:[:space:]]\@=/:/g

  • :\@!< 匹配任何非:,包括行首.这是否定前瞻/后视断言的一个重要特征.它并不要求实际上有一个字符,只是没有 :.

  • :\@!< matches any non-:, including the start of the line. This is an important characteristic of the negative lookahead/lookbehind assertions. It's not requiring that there actually be a character, just that there isn't a :.

: 匹配所需的冒号.

[^:[:space:]] 引入了更多的正则表达式概念.

[^:[:space:]] introduces a couple more regex concepts.

  • 外面的 [] 是一个集合.集合用于匹配其中列出的任何字符.但是,前导 ^ 否定匹配.所以,[abc123] 将匹配 a, b, c, 1,23,但 [^abc123] 匹配除这些字符以外的任何字符.

  • The outer [] is a collection. A collection is used to match any of the characters listed inside. However, a leading ^ negates that match. So, [abc123] will match a, b, c, 1, 2, or 3, but [^abc123] matches anything but those characters.

[:space:] 是一个字符类.字符类只能在集合中使用.[:space:] 不出所料,意味着任何空格.在大多数实现中,它直接与 C 库的 isspace 函数的结果相关.

[:space:] is a character class. Character classes can only be used inside a collection. [:space:] means, unsurprisingly, any whitespace. In most implementations, it relates directly to the result of the C library's isspace function.

将所有这些结合在一起,该集合的意思是匹配任何不是 : 或空格的字符".

Tying that all together, the collection means "match any character that is not a : or whitespace".

\@= 是正向前瞻断言.它适用于前一个原子(在本例中为集合),意味着该集合是模式成功匹配所必需的,但不会成为被替换文本的一部分.

\@= is the positive lookahead assertion. It applies to the previous atom (in this case the collection) and means that the collection is required for the pattern to be a successful match, but will not be part of the text that is replaced.

因此,每当模式匹配时,我们只需将 : 替换为它自身和一个空格.

So, whenever the pattern matches, we just replace the : with itself and a space.

这篇关于正则表达式在vim中插入空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆