在 Vim 中匹配单引号或双引号字符串

发布于 2024-11-06 08:22:07 字数 636 浏览 0 评论 0原文

我很难尝试将单引号或双引号字符串与 Vim 匹配 正则表达式引擎。

问题是我将正则表达式分配给一个变量,然后使用它 玩matchlist

例如,假设我知道我所在的行包含带引号的字符串,并且我想匹配它:

let regex = '\v"(.*)"'

这可以匹配任何双引号引起来的内容。类似地,这将匹配单引号字符串:

let regex = "\v'(.*)'"

但如果我尝试同时使用它们,例如:

let regex = '\v['|"](.*)['|"]'

or

let regex = '\v[\'|\"](.*)[\'|\"]'

那么 Vim 不知道如何处理它,因为它认为某些引号在实际变量定义中没有被关闭,这会造成混乱上正则表达式。

使用正则表达式捕获单引号或双引号字符串的最佳方法是什么?

也许(可能!)我错过了一些非常简单的东西,能够使用两个引号,而不用担心实际正则表达式的周围引号。

请注意,我更喜欢在正则表达式中使用单引号,因为这样我就不需要使用双反斜杠进行转义。

I am having a hard time trying to match single or double quoted strings with Vim's
regular expression engine.

The problem is that I am assigning the regular expression to a variable and then using that
to play with matchlist.

For example, let's assume I know I am in a line that contains a quoted string and I want to match it:

let regex = '\v"(.*)"'

That would work to match anything that is double-quoted. Similarly, this would match single quoted strings:

let regex = "\v'(.*)'"

But If I try to use them both, like:

let regex = '\v['|"](.*)['|"]'

or

let regex = '\v[\'|\"](.*)[\'|\"]'

Then Vim doesn't know how to deal with it because it thinks that some quotes are not being closed in the actual variable definition and it messes up the regular expression.

What would be the best way to catch single or double quoted strings with a regular expression?

Maybe (probably!) I am missing something really simple to be able to use both quotes and not worry about the surrounding quotes for the actual regular expression.

Note that I prefer single quotes for regular expression because that way I do not need to double-backslash for escaping.

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

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

发布评论

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

评论(3

明明#如月 2024-11-13 08:22:07

您需要使用反向引用。像这样:

let regex = '\([''"]\)\(.\{-}\)\1'

或者非常神奇

let regex = '\v([''"])(.{-})\1'

或者你可以使用(因为它不会扰乱你的子匹配):

let regex = '\%("\([^"]*\)"\|''\([^'']*\)''\)'

或者非常神奇:

let regex = '\v%("([^"]*)"|''([^'']*)'')'

You need to use back references. Like so:

let regex = '\([''"]\)\(.\{-}\)\1'

Or with very-magic

let regex = '\v([''"])(.{-})\1'

Alternatively you could use (as it will not mess with your sub-matches):

let regex = '\%("\([^"]*\)"\|''\([^'']*\)''\)'

or with very magic:

let regex = '\v%("([^"]*)"|''([^'']*)'')'
_失温 2024-11-13 08:22:07

看看这篇文章

Replacing quotemarks around strings in Vim?

可能会有所帮助以某种方式

look at this post

Replacing quote marks around strings in Vim?

might help in some way

淡忘如思 2024-11-13 08:22:07

这是我为引用字符串的语法编写的可行脚本。

syntax region myString start=/\v"/ skip=/\v(\\[\\"]){-1}/ end=/\v"/
syntax region myString start=/\v'/ end=/\v'/

您可以使用 \v(\\[\\"]){-1} 跳过某些内容。

This is a workable script I write for syntax the quoted strings.

syntax region myString start=/\v"/ skip=/\v(\\[\\"]){-1}/ end=/\v"/
syntax region myString start=/\v'/ end=/\v'/

You may use \v(\\[\\"]){-1} to skip something.

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