在 Vim 中匹配单引号或双引号字符串
我很难尝试将单引号或双引号字符串与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用反向引用。像这样:
或者非常神奇
或者你可以使用(因为它不会扰乱你的子匹配):
或者非常神奇:
You need to use back references. Like so:
Or with very-magic
Alternatively you could use (as it will not mess with your sub-matches):
or with very magic:
看看这篇文章
Replacing quotemarks around strings in Vim?
可能会有所帮助以某种方式
look at this post
Replacing quote marks around strings in Vim?
might help in some way
这是我为引用字符串的语法编写的可行脚本。
您可以使用
\v(\\[\\"]){-1}
跳过某些内容。This is a workable script I write for syntax the quoted strings.
You may use
\v(\\[\\"]){-1}
to skip something.