SublimeText 片段的正则表达式 [英] Regex for SublimeText Snippet

查看:56
本文介绍了SublimeText 片段的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经被这个 Sublime Snippet 困住了一段时间.

I've been stuck for a while on this Sublime Snippet now.

我想在创建新类时显示正确的包名称,使用 TM_FILEPATHTM_FILENAME.

I would like to display the correct package name when creating a new class, using TM_FILEPATH and TM_FILENAME.

打印 TM_FILEPATH 变量时,我得到如下信息:

When printing TM_FILEPATH variable, I get something like this:

/Users/caubry/d/[...]/src/com/[...]/folder/MyClass.as

我想转换这个输出,所以我可以得到类似的东西:

I would like to transform this output, so I could get something like:

com.[...].folder

这包括:

  • 删除 /com/[...]/folder/MyClass.as 之前的任何内容;
  • 删除 TM_FILENAME 及其扩展名;在这个例子中 MyClass.as;
  • 最后找到所有斜线并用点替换它们.

到目前为止,这就是我所拥有的:

So far, this is what I've got:

${1:${TM_FILEPATH/.+(?:src\/)(.+)\.\w+/\l$1/}}

然后显示:

com/[...]/folder/MyClass

我确实了解如何用点替换飞溅,例如:

I do understand how to replace splashes with dots, such as:

${1:${TM_FILEPATH/\//./g/}}

但是,我很难将此逻辑添加到前一个逻辑,以及删除逻辑末尾的 TM_FILENAME.

However, I'm having difficulties to add this logic to the previous one, as well as removing the TM_FILENAME at the end of the logic.

我对 Regex 非常缺乏经验,提前致谢.

I'm really inexperienced with Regex, thanks in advance.

:]

[...] 表示文件夹的可变数量.

[...] indicates variable number of folders.

推荐答案

我们可以通过一些技巧在一次替换中完成此操作.我们要做的是,将几个不同的案例放入我们的模式中,并对每个案例进行不同的替换.实现这一点的技巧是替换字符串必须不包含文字字符,而是完全由反向引用"组成.在这种情况下,那些没有参加比赛的组(因为他们是不同案例的一部分)将简单地作为空字符串写回,而不会对替换做出贡献.让我们开始吧.

We can do this in a single replacement with some trickery. What we'll do is, we put a few different cases into our pattern and do a different replacement for each of them. The trick to accomplish this is that the replacement string must contain no literal characters, but consist entirely of "backreferences". In that case, those groups that didn't participate in the match (because they were part of a different case) will simply be written back as an empty string and not contribute to the replacement. Let's get started.

首先,我们要删除直到最后一个 src/ 之前的所有内容(以模仿代码段的行为 - 如果您想删除第一个 src/之前的所有内容,请使用非贪婪量词):

First, we want to remove everything up until the last src/ (to mimic the behaviour of your snippet - use an ungreedy quantifier if you want to remove everything until the first src/):

^.+/src/

我们只是想删除它,所以不需要捕捉任何东西 - 也不需要写回任何东西.

We just want to drop this, so there's no need to capture anything - nor to write anything back.

现在我们要匹配后续文件夹直到最后一个.我们将捕获文件夹名称,也匹配尾随的 /,但写回文件夹名称和 ..但是我说替换字符串中没有文字!所以 . 也必须来自捕获​​.这里有一个假设,即您的文件始终具有扩展名.我们可以先行从文件名中获取句点.我们还将使用前瞻来确保前面至少还有一个文件夹:

Now we want to match subsequent folders until the last one. We'll capture the folder name, also match the trailing /, but write back the folder name and a .. But I said no literal text in the replacement string! So the . has to come from a capture as well. Here comes the assumption into play, that your file always has an extension. We can grab the period from the file name with a lookahead. We'll also use that lookahead to make sure that there's at least one more folder ahead:

^.+/src/|\G([^/]+)/(?=[^/]+/.*([.]))

我们将用 $1$2 替换它.现在,如果第一个替代捕获,组 $1$2 将为空,并且仍然删除前导位.如果第二个选择捕获,$1 将是文件夹名称,$2 将捕获一个句点.甜的.\G 是一个锚点,可确保所有匹配项彼此相邻.

And we'll replace this with $1$2. Now if the first alternative catches, groups $1 and $2 will be empty, and the leading bit is still removed. If the second alternative catches, $1 will be the folder name, and $2 will have captured a period. Sweet. The \G is an anchor that ensures that all matches are adjacent to one another.

最后,我们将匹配最后一个文件夹及其后的所有内容,并且只写回文件夹名称:

Finally, we'll match the last folder and everything that follows it, and only write back the folder name:

^.+/src/|\G([^/]+)/(?=[^/]+/.*([.]))|\G([^/]+)/[^/]+$

现在我们将用 $1$2$3 替换它作为最终解决方案.演示.

And now we'll replace this with $1$2$3 for the final solution. Demo.

概念上类似的变体是:

^.+/src/|\G([^/]+)/(?:(?=[^/]+/.*([.]))|[^/]+$)

替换为 $1$2.我真的只考虑了第二个和第三个选择的开始.演示.

replaced with $1$2. I've really only factored out the beginning of the second and third alternative. Demo.

最后,如果 Sublime 使用 Boost 的扩展格式字符串语法,实际上可以有条件地将字符放入替换中(无需从文件扩展名中神奇地变出它们):

Finally, if Sublime is using Boost's extended format string syntax, it is actually possible to get characters into the replacement conditionally (without magically conjuring them from the file extension):

^.+/src/|\G(/)?([^/]+)|\G/[^/]+$

现在我们有了第一个选项,直到 src(要删除),最后一个斜杠和文件名(要删除)的第三个选项,以及中间的您要保留的所有文件夹的替代方法.这次我把要替换的斜线选择性地放在开头.通过条件替换,我们可以在那里写一个 . 当且仅当该斜杠匹配时:

Now we have the first alternative for everything up to src (which is to be removed), the third alternative for the last slash and file name (which is to be removed), and the middle alternative for all folders you want to keep. This time I put the slash to be replaced optionally at the beginning. With a conditional replacement we can write a . there if and only if that slash was matched:

(?1.:)$2

不幸的是,我现在无法对此进行测试,而且我不知道使用 Boost 正则表达式引擎的在线测试人员.但这应该可以解决问题.

Unfortunately, I can't test this right now and I don't know an online tester that uses Boost's regex engine. But this should do the trick just fine.

这篇关于SublimeText 片段的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆