开发者

Matching text surrounded by whitespace in vim

I have a file with lines like this:

|   hello world   |       foo bar!   |

I want to be able to match the hello world and foo bar! so I can, say, reverse the two phrases without caring about the whitespace between phrases and pipe characters.

So far I have this, but is there a less verbose regex that will do the same thing?

:s/| \+\(.*[^ ]\) \+开发者_如何学编程| \+\(.*[^ ]\) \+|/| \2 | \1 |/g

Thanks!


If you don't care about the whitespace between, why not preserve it?

:s/\(|[^|]*\)\(|[^|]*\)|/\2\1|/g


You can use magic or very magic flags to use less slashes, but it doesn't help too much.

:s/\v\| +(.*[^ ]) +\| +(.*[^ ]) +\|/| \2 | \1 |/g

If you notice, you have the same regex twice in a row (\| +(.*[^ ]) +). You might be able to use that along with {2} to get it shorter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜