【问题标题】:Find last " in strings where " are written as ""在“写成”的字符串中查找最后一个“
【发布时间】:2016-11-17 13:47:52
【问题描述】:

我正在尝试编写一个 (Ruby) 正则表达式来捕获关闭 EViews 字符串的双引号。在 EViews 中,字符串用双引号括起来,为了在字符串中包含实际的双引号,我们编写双双引号:

myStr = "item1 item2 item3"
myStr2 = """item1"" ""item2"" ""item3"""

字符串可以出现在一行中的任何位置,因此使用行的开头和结尾对我没有帮助。

到目前为止我所拥有的是:

((?<="")|(?<!"))(")(?!")

即查找前面有两个双引号或没有双引号且后面没有双引号的双引号。这捕获了结束的双引号,但遗憾的是也捕获了开头的双引号。

顺便说一句,我需要这个的原因是我正在使用一个描述 Sublime Text 3 的 EViews 语法的 YAML 文件。至于捕获字符串,这是我迄今为止所拥有的:

strings:
  - match: '"'
    scope: punctuation.definition.string.begin.eviews
    push: string

string:
  - meta_scope: string.quoted.double.eviews
  - match: '"' #((?<="")|(?<!"))(")(?!")
    scope: punctuation.definition.string.end.eviews
    pop: true

【问题讨论】:

  • 您可以将这些字符串文字与"[^"]*(?:""[^"]*)*"匹配

标签: ruby regex string sublime-text-plugin eviews


【解决方案1】:

这个问题的措辞可能不是最好的。我最终得到了一个可能有点笨拙但有效的解决方案:

strings:
  - match: '"'
    scope: punctuation.definition.string.begin.eviews
    push: string

string:
  - meta_scope: string.quoted.double.eviews
  - match: '""'
  - match: '"'
    scope: punctuation.definition.string.end.eviews
    pop: true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2015-03-27
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多