Sublime Forum

Regex replace using back references

#1

Hi all,
I was trying to do a search and replace in Sublimetext 3, and I was using back references. But this does not seem to be supported in your regex engine.

Have I made a mistake in the attempts or is this not (yet) supported. If not supported, is this planned to be in a future build?

With kind regards,

Erik

0 Likes

#2

Sublime Text uses Boost regular expressions (docs.sublimetext.info/en/latest/ … xpressions), with additionally aliasing \<n> to $<n>.

Could you post your example text, expression and replace string for reproduction?

0 Likes

#3

Hi there,
Thanks of your reply. I was following examples found here: http://www.regular-expressions.info/named.html.

This was about <([A-Z][A-Z0-9])\b^>]>(.*?)</\1> which finds HTML tags. I was trying to insert a back reference in the Replace dialogue, but it did not put the back reference. Instead it literally inserted the back reference-text \2.

Does this help?

Regexbuddy and Regexmagic do state that “PCRE does not support search-and-replace at all.”

EditpadPro does, apparently http://www.regular-expressions.info/editpadpro.html, but does not mention which engine they use.

0 Likes

#4

I can’t see your problem. This pattern works perfect. But its a match pattern only, i can’t see what you want to replace. The pattern matches text, thats, what you want. With back reference \1 you match the tag.
You can test it in ST with the search & replace panel (Ctrl+H). Hit Alt+R to use regexp.

Please explain, what you want to replace.

0 Likes

#5

Stangg, do you have regular expressions enabled in the find dialog? It’s that button on the left. If you don’t, it’ll insert ‘\2’ instead of doing a regex replace.

0 Likes

#6

Hi all,
I re-tested, and indeed the replacement string does allow the use of back references.
The replacement string I tried was: Tag=\1 Content=\2

Then I altered the search by adding a named reference: <(?[A-Z][A-Z0-9])\b^>]>.*?</(\k)>

The replacement string Tag=\1 Content=\2 worked fine again. But this replacement string did not:
Tag=\k Content=\2

Can I not use the Named back reference?

0 Likes

#7

Please follow the specifications that I inked. You need to use $+{NAME} for named captures.

0 Likes