为什么我要逃出决赛]

发布于 2024-11-08 14:05:58 字数 639 浏览 0 评论 0原文

我有一个包含这样的字符串的文件:

print  $hash_xml->{'div'}{'div'}{'div'}[1]...

我想用其他内容替换 {'div'}{'div'}{'div'}[1]

所以我尝试了

%s/{'div'}{'div'}{'div'}[1]/by something else/gc

找不到字符串。我虽然必须转义 {,},[]

仍然找不到字符串。

所以我尝试搜索单个 { 并且它找到了它们。 然后我尝试搜索 {'div'}{'div'}{'div'},它再次找到了。
然后还是找到了{'div'}{'div'}{'div'}[1

要查找 {'div'}{'div'}{'div'}[1]

我必须使用 %s/{'div'}{'div'}{'div '}[1\]

为什么?

Linux 上的 vim 7.3

I have a file containing string like this one :

print  $hash_xml->{'div'}{'div'}{'div'}[1]...

I want to replace {'div'}{'div'}{'div'}[1] by something else.

So I tried

%s/{'div'}{'div'}{'div'}[1]/by something else/gc

The strings were not found. I though I had to escape the {,},[ and ]

Still string not found.

So I tried to search a single { and it found them.
Then I tried to search {'div'}{'div'}{'div'} and it found it again.
Then {'div'}{'div'}{'div'}[1 was still found.

To find {'div'}{'div'}{'div'}[1]

I had to use %s/{'div'}{'div'}{'div'}[1\]

Why ?

vim 7.3 on Linux

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

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

发布评论

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

评论(4

清秋悲枫 2024-11-15 14:05:58

[] 在正则表达式中用于包装一系列可接受的字符。

当两者都未转义时,vim 将搜索字符串视为正则表达式。

因此,当您省略它或转义最后一个字符时,vim 无法解释正则表达式上下文中的单个括号,文字搜索也是如此(基本上是给定搜索字符串的最好的结果)。

就我个人而言,我会避开左方括号和右方括号,以确保含义清晰。

The [] are used in regular expressions to wrap a range of acceptable characters.

When both are supplied unescaped, vim is treating the search string as a regex.

So when you leave it out, or escape the final character, vim cannot interpret a single bracket in a regex context, so does a literal search (basically the best it can do given the search string).

Personally, I would escape the opening and closing square brace to ensure that the meaning is clear.

我只土不豪 2024-11-15 14:05:58

这是因为 [] 字符用于构建搜索模式。

请参阅 :h pattern 并使用帮助文件 pattern.txt 尝试以下实验:

  • 搜索“[9-0]”模式(不带引号)使用 /[0-9] 将分别匹配从 0 到 9 的每个数字(请参阅 :h \[

  • 现在,如果您尝试 /\[0-9]/[0-9\] 您将匹配整个模式:方括号内的零、连字符和九。这是因为当您转义 [] 之一时,运算符 [*] 将不再存在。

使用您的搜索模式 /{'div'}{'div'}{'div'}[1\]/{'div'}{'div'}{'div '}\[1] 应匹配您想要的相同模式,而 /{'div'}{'div'}{'div'}[1] 匹配字符串 {'div'}{'div'}{'div'}1

That's because the [ and ] characters are used to build the search pattern.

See :h pattern and use the help file pattern.txt to try the following experiment:

  • Searching for the "[9-0]" pattern (without quotes) using /[0-9] will match every digit from 0 to 9 individually (see :h \[)

  • Now, if you try /\[0-9] or /[0-9\] you will match the whole pattern: a zero, an hyphen and a nine inside square brackets. That's because when you escape one of [ or ] the operator [*] ceases to exist.

Using your search pattern, /{'div'}{'div'}{'div'}[1\] and /{'div'}{'div'}{'div'}\[1] should match the same pattern which is the one you want, while /{'div'}{'div'}{'div'}[1] matches the string {'div'}{'div'}{'div'}1.

执妄 2024-11-15 14:05:58

为了避免被正则表达式中的这些特殊字符捕获,您可以尝试使用 very magic 标志。

例如:

:%s/\V{'div'}[1]/replacement/

注意行开头的 \V 标志。

In order to avoid being caught by these special characters in regular expressions, you can try using the very magic flag.

E.g.:

:%s/\V{'div'}[1]/replacement/

Notice the \V flag at the beginning of the line.

星星的轨迹 2024-11-15 14:05:58

因为方括号意味着 vim 认为你正在寻找里面的任何字符。这称为“字符类”。通过转义任一方括号,它可以让 vim 知道您正在寻找以 '[1]' 结尾的文字方字符串。

理想情况下,您应该将表达式编写为:

%s/{'div'}{'div'}{'div'}\[1\]/replacement string/

以确保含义完全清晰。

Because the square brackets mean that vim thinks you're looking for any of the characters inside. This is known as a 'character class'. By escaping either of the square brackets it lets vim know that you're looking for the literal square string ending with '[1]'.

Ideally you should write your expression as:

%s/{'div'}{'div'}{'div'}\[1\]/replacement string/

to ensure that the meaning is completely clear.

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