问题
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