Vim 中的正则表达式

发布于 2024-12-09 07:49:13 字数 409 浏览 0 评论 0原文

我已经使用 Vim 一段时间了,每次使用时我都会意识到它有多么强大。我知道的一件事是,每次使用都会让我获得关于新命令的全新学习体验,一旦我了解它们,这些命令似乎总是派上用场。

我注意到 SO 的一件事是很多用户在正则表达式中回答 Vim 问题。像这样的东西: :s/\(\S\+\)/"\1"/ (据说这是一个很棒的正则表达式)。所以我对此有两个问题。

  • (a) 如果我不使用正则表达式,我是否就无法充分利用 Vim 的功能?难道没有命令可以完成任何正则表达式会做的事情吗?

  • (b) 如果您确实认为正则表达式值得学习,请向我指出从入门到高级的阅读材料。如果有帮助的话,我主要使用 Vim 来编写 TeX 文件和 Python 脚本。

感谢社区,你们太棒了。

I have been using Vim for a while now, and I've come to realize how powerful it is each time I do so. One thing that I know is that each usage leads me to a whole new learning experience about new commands that always seem to come in handy once I know them.

One thing I notice coming to SO is that a lot of users answer Vim questions in regexps. Things like this: :s/\(\S\+\)/"\1"/ (supposedly this is an awesome regexp). So I have two questions regarding this.

  • (a) Am I not harnessing the full power of Vim if I don't use regexps? Aren't there commands to do pretty much what any regexp would do?

  • (b) If you do think regexps are worth learning, please point me to reading material ranging from intro to advanced levels. If it helps, I use Vim mostly for writing up TeX files and Python scripts.

Thanks SO community, you rock.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞 2024-12-16 07:49:13

a) 在我看来,如果您不使用它并且您对现在的工作方式感到满意,那么就不要使用它。一旦时机成熟,您开始需要正则表达式来做某事,您就会学习它们。到你需要的程度。大多数人都以这种方式使用它们。

很少有人了解它们,以至于他们可以编写自己的语法文件或类似的文件。

b) Vim 有自己独特的正则表达式风格(其中之一),但是对于首先,当然除了 Vim 的帮助之外,我还会推荐一本 Perl 入门书籍。例如,学习 Perl。它有一种很好的温和方法,使表达式开始有意义(并且您不只是将它们作为手头某个特定问题的衬托来学习)。 Perl 入门(免费版本!)可以在这里找到

关于正则表达式的高级书籍是掌握正则表达式,以及该系列的类似书籍。另外,中级和掌握 Perl 也不错。

a) In my opinion, if you're not using it and you're happy the way you're working now, leave it at that. Once the time comes and you start needing regular expressions for something you'll learn them. To the extent you need. Most people use them in just that way.

Very few know them to the point where they, let's say go writing their own syntax files or something similar.

b) Vim has its own particular flavour of regular expressions (one of many), but for a start, apart from Vim's help of course, I'd recommend one of the introductory books to Perl. For example, Learning Perl. It has a nice gentle approach so that expressions start making sense (and you don't just learn them as one liners for some particular problem at hand). Beginning Perl (free version !) can be found here.

Advanced books on Regexes are Mastering Regular Expressions, and similar of the series. Also, Intermediate and Mastering Perl are not bad.

不爱素颜 2024-12-16 07:49:13

本质上,正则表达式对于搜索和替换最有用。通过搜索有问题的文本并巧妙地将其替换为所需的文本,可以解决许多问题。这并不总是解决问题的最佳方法,也很少是唯一的方法。
正则表达式会找到一个模式,因此如果您想在许多具有相似模式的地方编辑文本,您通常可以使用正则表达式来识别它们
例如:

/my name is .*

会找到“我的名字是”
“我叫乔”
或任何以“我的名字是”开头的内容
. 匹配任何字符,* 告诉 vim 匹配 0 到前一个符号的无穷大(在本例中为 . 或任何字符)
这可能是最简单的正则表达式形式。对于更高级的用法,请使用 Idigas 提供的答案

Essentially Regular expressions are most useful for search and replaces. Many problems can be solved by searching for the offending text and replacing it in a clever manner with desired text. That is not always the best way to solve and rarely is it the only way.
Regular expressions find a pattern so if you are wanting to edit text in a bunch of places that has a similar pattern you can often identify them with a regular expression
Ex:

/my name is .*

Will find "my name is "
"my name is Joe"
or anything else that starts with "my name is "
the . matches any character and the * tells vim to match 0 to infinity of the previous symbol (in this case . or anything)
this is probably the simplest form of regex. for more advanced usage use the answers provided by Idigas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文