Sublime Text 2 regex matching with >9 back-referen

2019-02-22 11:20发布

问题:

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.

回答1:

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.



回答2:

You need to put braces around

${10}

Make this habit!

It becomes even more helpful when you actually want to append $1 with a number 0. Then you may require ${1}0