Sublime Text 2 regex 匹配 >9 反向引用 [英] Sublime Text 2 regex matching with >9 back-references

查看:35
本文介绍了Sublime Text 2 regex 匹配 >9 反向引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个字符串中捕获多个组并使用正则表达式对它们重新排序.但是,一旦我达到 9 个以上的反向引用,它们就不会像我预期的那样替换.

例如:这是一个有点人为的例子,但它应该说明正在发生的事情.

<前>输入字符串:abcdefghij查找内容:^(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)$替换为:\10预期输出:j实际输出:a0

我也试过:

<前>替换为:10 美元

不是 \10$10 插入第十个分组括号的内容,而是将第一个分组括号跟随者的内容插入0".

更新:

这在较新版本的 Sublime Text 中使用 $10 确实有效,但最好使用如下所述的 ${10} 语法.

解决方案

使用:

${10}

我刚试过,对我有用.

我也尝试过使用 find:

^(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)$

替换为:$10

它奏效了.但是,我使用的是 v2.0.2 build 2221.与 \10 相同不起作用,但如您在问题中描述的那样工作.

值得一提的是,许多正则表达式引擎正在从 \# 格式转变为 $# 格式,所以这可能是 的原因即使没有大括号,$# 对我来说效果更好.

尽管现在,如果您想用第 10 个捕获后跟一个数字(例如 $100)替换,引擎将查找第 100 个捕获组,而在您的示例中,将不替换任何内容.

I'm trying to capture several groups within a string and reorder them using a regex. However once I reach greater then 9 back-references, they do not replace as I expected.

Eg: This is a bit of a contrived example, but it should illustrate what's happening.

Input string: abcdefghij
Find What: ^(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)$
Replace With: \10
Expected output: j
Actual output: a0

I also tried:

  Replace With: $10

Instead of \10 or $10 inserting the contents of the tenth grouping parens, it inserts the contents of the first grouping parens follower by "0".

UPDATE:

This does work using $10 in newer versions of Sublime Text, but it is way better to use the ${10} syntax as described below.

解决方案

Use:

${10}

I just tried it and that worked for me.

I also tried using find:

^(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)$

Replace with: $10

And it worked. I'm using v2.0.2 build 2221 however. The same with \10 doesn't work, but works as you described in your question.

It might be worth noting as well that many of the regex engines replace are shifting from the \# format to the $# format so that's maybe why $# is working better for me even without braces.

Though now, if you wanted to replace with the 10th capture followed by a number (e.g. $100) the engine will look for the 100th capture group and for your example, will replace with nothing.

这篇关于Sublime Text 2 regex 匹配 &gt;9 反向引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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