我可以在 vscode 的代码片段中排除正面前瞻和后视吗?

Can I exclude Positive Lookaheads and Lookbehinds within a snippet in vscode?

我在排除 VSCode 代码段中的部分字符串时遇到问题。本质上,我想要的是一条特定的路径,但我无法让正则表达式排除我需要排除的内容。

我最近问了一个类似的问题,您可以在这里找到:

如您所见,我主要是被片段在 vscode 中的工作方式绊倒了,而不是正则表达式本身

${TM_FILEPATH/(?<=area)(.+)(?=state)/${1:/pascalcase}/}

给定一个看起来像 abc/123/area/my-folder/state/...

的文件路径

预计:

/MyFolder/

实际:

abc/123/areaMyFolderstate/...

您需要匹配整个字符串才能实现:

"${TM_FILEPATH/.*area(\/.*?\/)state.*/${1:/pascalcase}/}"

regex demo

详情

  • .* - 除换行字符外的任何 0+ 个字符,尽可能多
  • area——一句话 -(\/.*?\/) - 第 1 组:/,除换行字符外的任何 0+ 个字符,尽可能少,以及 / -state.* - state 子字符串和该行的其余部分。

注意:如果areastate之间必须没有其他子部分,请将.*?替换为[^\/]*或甚至 [^\/]+.

预期的输出似乎与输入中的部分字符串不同。如果需要,表达式可能会非常复杂,例如:

(?:[\s\S].*?)(?<=area\/)([^-])([^-]*)(-)([^\/])([^\/]*).*

并替换类似于 /\U\E\U\E/ 的内容(如果可用)。

Demo 1

如果还有其他操作,现在我猜 pascalcase 可能会做一些事情,这个简单的表达式可能只是在这里工作:

.*area(\/.*?\/).*

并且所需的数据在此捕获组中 </code>:</p> <pre><code>(\/.*?\/)

Demo 2

根据您在问题中链接到的我的回答,请记住环视是 "zero-length assertions" 和 "do not consume characters in the string"。见 lookarounds are zero-length assertions:

Lookahead and lookbehind, collectively called "lookaround", are zero-length assertions just like the start and end of line, and start and end of word anchors explained earlier in this tutorial. The difference is that lookaround actually matches characters, but then gives up the match, returning only the result: match or no match. That is why they are called "assertions". They do not consume characters in the string, but only assert whether a match is possible or not.

所以在您的代码片段转换中:/(?<=area)(.+)(?=state)/环视部分实际上并未被消耗,因此只是通过了。 Vscode 将它们视为实际上根本不在 "part to be transformed" 段内。

这就是环视不排除在您的转换之外的原因。