Sublime Forum

Replace regex capture group instead of reusing them

#1

I’m creating a long regex to restructure some bank statements into a CSV format and making some other adjustments as well. I’d prefer not to use multiple Regexes as I have a lot of files to process. So far it is working well however I’m at a point where I’d like to replace a capture group with some other text but I’m not sure if that is even possible.

To be more specific, I would like to search for the months as ( Jan)( Feb)( Mar)* etc. with the expectation that these would be capture groups $1, $2, $3 etc. and replace them with “/01/2024”, “/02/2024”, “/03/2024” etc. Where “Jan” would be replaced with “/01/2024” instead of using $1.

For example:

Data:
29 Jul Some long text that will be restructured $440.00 $2,033.80 CR
30 Aug Some other text that will be restructured $21.00 $2,054.80 CR
30 Oct Some more sample text that will be restructured $26.65 $2,081.45 CR

Expected output:
29/07/2024 Some long text that will be restructured $440.00 $2,033.80 CR
30/08/2024 Some other text that will be restructured $21.00 $2,054.80 CR
30/10/2024 Some more sample text that will be restructured $26.65 $2,081.45 CR

Is there a way to do this using Regex in Sublime Text’s Find and Replace?

0 Likes

#2

I believe this is still not possible using the Find and Replace panel until https://github.com/sublimehq/sublime_text/issues/1524 is implemented.
As a workaround, maybe use multiple replacements (maybe via a macro?) or just find/write a simple python plugin for it

Note: there is a hack that could let you do the replacements using snippets after a Find All operation:
https://stackoverflow.com/a/78502220/4473405

0 Likes

#3

RegReplace might help too

0 Likes

#4

Thanks @kingkeith, that is very informative and helpful. Hopefully it will be implemented eventually. I haven’t actually explored any additional tools in Sublime Text except a plugin to tidy up code and I’ve never actually learned python. It’s on my to-do list.
Thanks @TheSecEng, that looks like a great tool. I’ll take a look at it as that might just give me the flexibility I need.
Also, thanks to both of you for a positive introduction to this forum as this is my first post. :smiley:

1 Like