简体   繁体   English

TextMate 变换以替换匹配重复组中的单个字符

[英]TextMate transform to replace single character in matched repetitive group

TL;DR show me a general vscode snippet to transform "/home/me/projects/project/src/Commands/Interfaces/Whatever" to "Commands\Interfaces\Whatever" TL;DR 向我展示了一个通用的 vscode 片段,用于将“/home/me/projects/project/src/Commands/Interfaces/Whatever”转换为“Commands\Interfaces\Whatever”

Hi all, I'm using Vscode and trying to figure out a simple transform of the file directory to insert the PHP namespace.大家好,我正在使用 Vscode 并试图找出文件目录的简单转换以插入 PHP 命名空间。 So far i could not figure out a simple, general solution to this seemingly mundane problem:到目前为止,我无法为这个看似平凡的问题找到一个简单、通用的解决方案:

  • Is there a way to transform PART of the TM_DIRECTORY variable in a snippet, so that every forward slash becomes a backward slash?有没有办法在片段中转换 TM_DIRECTORY 变量的一部分,以便每个正斜杠都变成反斜杠?

This seems so trivial at first, right?乍一看,这似乎很微不足道,对吧? start from从...开始

${TM_DIRECTORY/\\//\\\\/}

Then buid up from there.然后从那里建立起来。 Problem is, I want to catch only part of the filename, THEN transform slashes on the catched group, without resorting to a fixed/maximum number of filename components (in that case, there is an ugly, lengthy solution)问题是,我只想捕获文件名的一部分,然后在捕获的组上转换斜杠,而不使用固定/最大数量的文件名组件(在这种情况下,有一个丑陋、冗长的解决方案)

Is the following in the right direction?以下是正确的方向吗? If so, what replacement string results in group $1 but with slashes reverted?如果是这样,什么替换字符串会导致组 $1 但斜杠恢复?

${TM_DIRECTORY/.*\\/src(\\/([^\\/]+))+$/??????/}

Thanks in advance, an acceptable response is "there is just no way unless you relax your requirements", this is more of a theoretical question on the power of regexes.在此先感谢,一个可以接受的回答是“除非你放松你的要求,否则没有办法”,这更像是一个关于正则表达式力量的理论问题。

You can use您可以使用

"${TM_DIRECTORY/^.*?[\\\\\\/]src[\\\\\\/]|([\\\\\\/])/${1:+\\\\}/g}"

See the regex demo (there is an extra \ at the start of the resulting string as there is no way to introduce the conditional replacement pattern as in VS Code).请参阅正则表达式演示(在结果字符串的开头有一个额外的\ ,因为无法像 VS Code 中那样引入条件替换模式)。

Details :详情

  • ^ - start of string ^ - 字符串的开头
  • .*? - any zero or more chars other than line break chars, as few as possible - 除换行符之外的任何零个或多个字符,尽可能少
  • [\\\/] - / or \ char [\\\/] - /\字符
  • src - the directory name up to which you need to remove the subdirs src - 您需要删除子目录的目录名称
  • [\\\/] - a / or \ char [\\\/] - 一个/\字符
  • | - or - 或者
  • ([\\\/]) - Group 1: a / or \ char. ([\\\/]) - 第 1 组:一个/\字符。

The ${1:+\\} replacement means we replace with \ only when Group 1 match occurs. ${1:+\\}替换意味着我们仅在第 1 组匹配发生时才替换为\

Note the backslashes need doubling, and g at the end replaces all occurrences.请注意,反斜杠需要加倍,最后的g替换所有出现。

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

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