简体   繁体   English

在以“”表示的字符串中找到最后一个“”

[英]Find last “ in strings where ” are written as “”

I'm trying to write a (Ruby) regex for capturing the double quote that closes an EViews string. 我正在尝试编写一个(Ruby)正则表达式来捕获关闭EViews字符串的双引号。 In EViews, strings are enclosed by double quotes and to include actual double quotes in a string, we write double double quotes: 在EViews中,字符串用双引号引起来,并且为了在字符串中包含实际的双引号,我们编写双双引号:

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

Strings can appear wherever on a line, so I'm not helped by using beginnings and endings of lines. 字符串可以出现在一行中的任何位置,因此使用行的开头和结尾对我没有帮助。

What I have so far is: 到目前为止,我有:

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

That is, find a double quote that is preceded by either two double quotes or no double quote and is not succeeded by a double quote. 即,找到一个双引号,该双引号前面有两个双引号或没有双引号,并且后没有双引号。 This captures the closing double quote, but sadly also the opening one. 这捕获了结尾的双引号,但令人遗憾的是开头的双引号。

On a side note, the reason that I need this is that I'm working with a YAML file describing EViews syntax for Sublime Text 3. As for capturing strings, this is what I have thus far: 附带说明一下,我需要这样做的原因是我正在使用一个YAML文件,该文件描述了Sublime Text 3的EViews语法。至于捕获字符串,这就是我到目前为止所拥有的:

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

This question was perhaps not phrased in the best way. 这个问题也许不是用最好的方式表达的。 I ended up with a solution that perhaps is a bit clumsy, but works: 我最终得到了一个可能有点笨拙但可以解决的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM