简体   繁体   English

匹配不用大括号括起来的单个字符

[英]Match single character not enclosed by braces

I am making a property list syntax definition file ( tmLanguage ) for practice. 我正在制作一个属性列表语法定义文件( tmLanguage )进行练习。 It's in Sublime Text's YAML format, but I'll be using it in VS Code. 它采用Sublime Text的YAML格式,但我将在VS Code中使用它。

I need to match all characters (including unterminated { and } ) that are not enclosed by {} . 我需要匹配所有字符(包括无端接{} )不是由封闭的{}

I have tried using negative lookahead and lookbehind assertions, but it just matches not the first or last character in brackets. 我尝试使用否定的前瞻性和后向断言,但它仅与括号中的第一个或最后一个字符不匹配。

(?<!{).(?!})

Adding a greedy quantifier to consume all characters just matches the full line. 添加贪婪的量词以消耗所有字符正好匹配整行。

(?<!{).+(?!})

Adding a lazy quantifier just matches everything except the first character after the { . 添加惰性量词仅匹配{后面的第一个字符之外的所有字符。 It also matches {} exactly. 它还与{}完全匹配。

(?<!{).+?(?!})

| Test              | Expected Matches        |
| ----------------- | ----------------------- |
| `{Ctrl}{Shift}D`  | `D`                     |
| `D{Ctrl}{Shift}`  | `D`                     |
| `{Ctrl}D{Shift}`  | `D`                     |
| `{Ctrl}{Shift}D{` | `D` `{`                 |
| `{Ctrl}{Shift}D}` | `D` `}`                 |
| `D}{Ctrl}{Shift}` | `D` `}`                 |
| `D{{Ctrl}{Shift}` | `D` `{`                 |
| `{Shift`          | `{` `S` `h` `i` `f` `t` |
| `Shift}`          | `S` `h` `i` `f` `t` `}` |
| `{}`              | `{` `}`                 |

Sample text file: https://raw.githubusercontent.com/spikespaz/windows-tiledwm/master/hotkeys.conf 示例文本文件: https : //raw.githubusercontent.com/spikespaz/windows-tiledwm/master/hotkeys.conf


Full syntax highligher: 完整语法highligher:

# [PackageDev] target_format: plist, ext: tmLanguage
---
name: Hotkeys Config
scopeName: source.hks
fileTypes: ["hks", "conf"]
uuid: c4bcacab-0067-43db-a1d7-7a74fffe2989

patterns:
- name: keyword.operator.assignment
  match: \=
- name: constant.language
  match: "null"
- name: constant.numeric.integer
  match: \{(?:Alt|Ctrl|Shift|Super)\}
- name: constant.numeric.float
  match: \{(?:Menu|RMenu|LMenu|Tab|Enter|PgUp|PgDown|Ins|Del|Home|End|PrntScr|Esc|Back|Space|F[0-12]|Up|Down|Left|Right)\}
- name: comment.line
  match: \#.*
...

You can use the following RegEx to match: 您可以使用以下RegEx进行匹配:

(?:{(Ctrl|Shift|Alt)})*

Then simply replace the matches with an empty string and what you get is according to your wishes. 然后只需匹配项替换为空字符串,您将根据自己的意愿获得匹配。

The RegEx is selfexplaining, but here's a short description: RegEx是不言自明的,但是这里有个简短的描述:

It creates a non capturing Group consisting of one of your modifier keys in curly brackets. 它会创建一个non capturing Group ,该non capturing Group由大括号中的一个修饰键组成。 The plus sign ' + ' at the right means it repeats that one or more times. 右侧的加号' + '表示重复一次或多次。

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

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