简体   繁体   English

从perl的角度来看vim regexp:用反斜杠转义的特殊字符

[英]vim regexps from the perl's point of view: which special characters to escape with backslash

Imagine, we have to construct a regexp in vi/vim. 想象一下,我们必须在vi / vim中构造一个正则表达式。 Which special characters we have to escape with backslash? 我们必须使用反斜杠转义哪些特殊字符?

By special characters I mean the following chars: {}|()-[]+*.^$? 特殊字符是指以下字符:{} |()-[] + *。^ $ ??

Seems like we have to escape: {|()+? 似乎我们必须逃脱:{|()+?

And leave as is: }^$*.[]- 并保留原样:} ^ $ *。[]-

Thanks. 谢谢。

ps AFAIK, we have no '?' ps AFAIK,我们没有“?” character in vi/vim but '=' instead which should be also escaped by backslash. vi / vim中的字符,但应使用反斜杠转义'='。

我认为Vim上有关魔术角色的文档将为您提供明确的清单。

In vim: 在vim中:

:help magic :帮助魔术

I think the simplest way, that will work regardless of vim's magic setting, is 我认为不管vim的magic设置如何,最简单的方法就是

let re = '\V' . escape(fixed_string, '\')

\\V disables magic entirely from that point onward in the RE. \\V从那时起完全禁用魔法。 This means that anything not preceded by a single backslash (well, technically by an odd number of backslashes) is a normal character. 这意味着任何未加单个反斜杠(从技术上讲,奇数个反斜杠)的字符都是正常字符。 Since we escape any backslashes in the fixed string, there can be no magic characters contained in it. 由于我们在固定字符串中转义了任何反斜杠,因此其中不能包含魔术字符。

Remember that with \\V in effect, you have to backslash-prefix any RE meta-characters. 请记住,启用\\V后,必须对所有RE元字符加反斜杠前缀。 So if you're looking for a line that entirely consists of the fixed string, you would 因此,如果您要查找完全由固定字符串组成的行,则将

let full_line_re = '\V\^' . escape(fixed_string, '\') . '\$'

Also keep in mind that vim's ignorecase and smartcase settings will affect RE behaviour. 还请记住,vim的ignorecasesmartcase设置会影响RE的行为。 The \\C (case-sensitive) and \\c (case-insensitive) switches work the same way as \\V : they will override those settings for the part of any regexp which follows them. \\C (区分大小写)和\\c (不区分大小写)开关的工作方式与\\V相同:它们将覆盖跟在它们后面的任何正则表达式部分的设置。

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

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