Home Download Buy Blog Forum Support

Accessing named regex groups in find/replace?

Accessing named regex groups in find/replace?

Postby ketura on Sat Jun 21, 2014 5:50 pm

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 (?<name>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<name> 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}, $<name>, \g<name>, 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?
ketura
 
Posts: 3
Joined: Sun Jun 03, 2012 6:38 pm

Re: Accessing named regex groups in find/replace?

Postby ntenney on Tue Jun 24, 2014 7:15 pm

ketura wrote: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 (?<name>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?


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}.
ntenney
 
Posts: 66
Joined: Thu May 03, 2012 9:10 pm

Re: Accessing named regex groups in find/replace?

Postby ketura on Tue Jun 24, 2014 7:51 pm

Excellent! Thank you so much!

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

Thanks again!
ketura
 
Posts: 3
Joined: Sun Jun 03, 2012 6:38 pm

Re: Accessing named regex groups in find/replace?

Postby ntenney on Wed Jun 25, 2014 3:02 am

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.
ntenney
 
Posts: 66
Joined: Thu May 03, 2012 9:10 pm


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 40 guests