假设我有以下内容markdown
列出项目:
- [x] Example of a completed task.
- [x] ! Example of a completed task.
- [x] ? Example of a completed task.
我有兴趣使用解析该项目regex
并提取以下组捕获:
-
$1
: 左边[
和右边的]
当符号为括号时x
介于两者之间
-
$2
: 符号x
在括号之间[
and ]
-
$3
: 修饰符!
接下来是[x]
-
$4
: 修饰符?
接下来是[x]
-
$5
: 后面的文字[x]
没有修饰符,例如[x] This is targeted.
-
$6
: 后面的文字[x] !
-
$7
: 后面的文字[x] ?
经过使用在线解析器的大量试验和错误后,我得出以下结论:
((?<=x)\]|\[(?=x]))|((?<=\[)x(?=\]))|((?<=\[x\]\s)!(?=\s))|((?<=\[x\]\s)\?(?=\s))|((?<=\[x\]\s)[^!?].*)|((?<=\[x\]\s!\s).*)|((?<=\[x\]\s\?\s).*)
为了使regex
上面更具可读性,这些是一一列出的捕获组:
-
$1
: ((?<=x)\]|\[(?=x]))
-
$2
: ((?<=\[)x(?=\]))
-
$3
: ((?<=\[x\]\s)!(?=\s))
-
$4
: ((?<=\[x\]\s)\?(?=\s))
-
$5
: ((?<=\[x\]\s)[^!?].*)
-
$6
: ((?<=\[x\]\s!\s).*)
-
$7
: ((?<=\[x\]\s\?\s).*)
这很可能不是最好的方法,但至少它似乎捕捉到了我想要的:
我想延长这一点regex
捕获线markdown
表那个
看起来像这样:
| | Task name | Plan | Actual | File |
| :---- | :-------------------------------------- | :---------: | :---------: | :------------: |
| [x] | Task one with a reasonably long name. | 08:00-08:45 | 08:00-09:00 | [[task-one]] |
| [x] ! | Task two with a reasonably long name. | 09:00-09:30 | | [[task-two]] |
| [x] ? | Task three with a reasonably long name. | 11:00-13:00 | | [[task-three]] |
更具体地说,我对具有与上面相同的组捕获感兴趣,但我想排除表格网格(即|
)。所以,团体$1
to $4
应该保持不变,但是组$5
to $7
应捕获文本,不包括|
,例如,如下面的选择所示:
您对我如何调整有任何想法吗,例如,组的正则表达式$5
排除|
。我无休止地尝试了各种否定(例如,[^\|]
)。我在用Oniguruma 正则表达式 https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#textmate-grammars.