How to match operator-separated strings in Sublime Package Development YAML tmlanguage

此生再无相见时 提交于 2019-12-12 03:28:20

问题


I am making a syntax definition for a custom-made language in sublime text 2 using PackageDevelopment's .YAML-tmLanguage. For now I want my syntax to identify strings to non strings.

sample line of code:

string name = "Chuck Norris";
string message = "I am " + name + ", don't mess with a \"ROCKSTAR\"!";

my pattern for double quoted string:

- comment: strings in double quotes
  match: (".+")
  captures:
    '1': {name: string.quoted.double.me}

what the pattern captures:

string name = "Chuck Norris";
string message = "I am " + name + ", don't mess with a "ROCKSTAR"!";

line 1 above is correct but line 2 seems to capture all.

what I want is:

string name = "Chuck Norris";
string message = "I am " + name + ", don't mess with a "ROCKSTAR"!";


回答1:


You need to match all characters that are not " and also combinations of \+anything in between the quotes.

Use

"[^"\\]*(?:\\.[^"\\]*)*"

See this regex demo

It can also be written as "(?:[^"\\]|\\.)*". It is easier to read but is less efficient.




回答2:


I discovered a good implementation on how to match operator-separated strings and also to match double quoted strings and some additional functionalities using YAML.

@Wiktor Stribiżewv's answer also fulfills the question but I found a good implementation for this:

- comment: strings in double quotes
  name: string.quoted.double.hit
  begin: \"
  end: \"
  patterns:
  - comment: escape characters
    name: constant.character.escape.hit
    match: \\.

  - comment: special characters
    name: constant.character.hit
    match: \%.

this also matches escape characters like \", \n and special characters %s, %d



来源:https://stackoverflow.com/questions/36646907/how-to-match-operator-separated-strings-in-sublime-package-development-yaml-tmla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!