我该如何修复 Emacs? qr“\.[^.]+$”的语法高亮?
一个字符串之后的语法令人困惑
fileparse($file, qr"\.[^.]+$");
Emacs 在像And 认为文件的其余部分是 。我该如何解决这个问题?
Emacs is confusing syntax after a line like
fileparse($file, qr"\.[^.]+$");
And thinks the rest of the file is a string. How do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Emacs 对 qr 表达式中的引号感到困惑。标准正则表达式分隔符 (/) 有效:
事实上,几乎其他任何内容似乎都有效。
我的 VIM 版本不会被引号混淆,但我知道旧版本的 VIM 会被引号混淆。添加引号确实令人困惑,因为它使
qr
表达式看起来像一个字符串(但事实并非如此)。在正则表达式中使用引号作为分隔符通常是不好的策略,即使它在技术上是合法的。然而,真正重要的问题是
fileparse
是什么?这不是标准的 Perl 函数。我假设这是从File::Basename
导入的?那是正确的吗?根据我的文档,fileparse 中的第二个参数应该是一个数组,而不是带引号的正则表达式。
Emacs is confused by the quotes in the
qr
expression. The standard regular expression delimiter (/) works:In fact, almost anything else seems to work.
My version of VIM doesn't get confused by the quotes, but I know that older versions of VIM did. Putting quotes is really confusing because it makes the
qr
expression look like a string (which it isn't). It's usually bad policy to use quotes as delimiters in regular expressions even though it's technically legal.However, the really important question is what's
fileparse
? That's not a standard Perl function. I am assuming that this is imported fromFile::Basename
? Would that be correct?According to the documentation I have, the second argument in
fileparse
is suppose to be an array and not a quoted regular expression.David W. 可能有正确的答案。同样,最好避免使用
"
作为分隔符,但有时您需要使用'
作为分隔符以防止插值,因此这对于这种情况下的人可能有用。因此,如果出于某种原因你真的想要引号分隔符(或者为了防止使用
'
进行插值),你总是可以这样做:一个使用 SO 做得不好的示例:
注意:第二个
#
是触发编辑器中的注释突出显示,以便highlight fix
显示为注释。David W. probably has the right answer. Again, probably best to avoid using
"
as delimiter, but sometimes you need to use'
as the delimiter to prevent interpolation, therefore this may be useful to someone in that case.So, if for some reason you REALLY want the quote delimiter (or to prevent interpolation using
'
), you can always do something like:an example using one that SO does badly:
note: the second
#
is to trigger the comment highlighting in the editor so thathighlight fix
shows as a comment.