Sublime Forum

Accessing named regex groups in find/replace?

#1

I have recently been dealing with some incredibly long regexes used to manipulate a CSV, so the regexes are set up to group each column as its own group. However, there’s a snag as \10 in the replace field is interpreted as group 1, literal 0, rather than the 10th captured group. Upon discovering this limitation, I looked into using named groups with the (?group) syntax. The group appears to be captured (which is to say, the syntax is parsed and the appropriate group is highlighted in the file), but I can’t seem to find which syntax is supposed to be used to reference these named groups in the replace field.

I’ve attempted both \g{name}, and \k as suggested on the boost (perl) syntax documentation which Sublime supposedly uses, but to no avail. I’ve also attempted {name}, \name, <name>, $name, ${name}, $, \g, and \k{name}, and each of these is interpreted literally.

I’m at a loss. Are named groups accessible at all within the replace field? Failing that, is there some way to refer to numbered groups larger than \9?

0 Likes

#2

[quote=“ketura”]I have recently been dealing with some incredibly long regexes used to manipulate a CSV, so the regexes are set up to group each column as its own group. However, there’s a snag as \10 in the replace field is interpreted as group 1, literal 0, rather than the 10th captured group. Upon discovering this limitation, I looked into using named groups with the (?group) syntax. The group appears to be captured (which is to say, the syntax is parsed and the appropriate group is highlighted in the file), but I can’t seem to find which syntax is supposed to be used to reference these named groups in the replace field.

I’m at a loss. Are named groups accessible at all within the replace field? Failing that, is there some way to refer to numbered groups larger than \9?[/quote]

I don’t know about named groups, but to access numbered groups higher than 9, you can use ${number}. So, for your example, you could use ${10} to access the tenth group from your csv.

EDIT
Also, it looks like the correct syntax for using named groups in a replace is $+{name}.

3 Likes

#3

Excellent! Thank you so much!

I wonder why they used such inconsistent syntax…but oh well, so long as it works.

Thanks again!

0 Likes

#4

Well, actually the $+{name} syntax is perl, which is what the boost library is based off of, so that’s why I gave it a try.

0 Likes

#5

Thanks!! Difficult to find, this one.

0 Likes

#6

Thanks for clarifying. I found another issue referencing this page, and posting the syntax for both search and replace:

Capturing for example all words
(?<named_capture_group>\w+)

Inserting a dash after all words
$+{named_capture_group}-

Text example from the linked issue

Title: Hamlet
Genre: Tragedy
Playwright: Williams Shakespere

Title: Woyzeck
Genre: Tragedy
Playwright: Karl Georg Büchner

Find
Playwright: (?<playwright>.*)

Replace
<playwright>$+{playwright}</playwright>

1 Like

#7

The gift that keeps giving 10 years later. Thanks folks!

0 Likes