1

Regex will find matches that satisfy the expression but are different. How to you refer to the result instance in order to append to each instance? I have created a text file by copy/paste from a website that does not provide any sort of file download. The file I created contains a lot of text and numbers that I want to put into a spreadsheet. The numbers are of the form nn.nn and separated by spaces. Unfortunately the text also has spaces. Therefore the spaces cannot be used as a delimiter. I am using the ‘Sublime’ text editor and the regex expression I am using is ‘\d\d.\d\d’ which identifies the numbers I am after. I then want to append a comma after each nn.nn figure in order to import the text file into a spreadsheet.

The ‘Find’ tool works correctly in Sublime but the ‘Replace’ tool does just that. I want an ‘Append’ tool as there does not seem to be a way of referring to the text that has been found in the ‘Replace’ field so that I could enter ‘[what was found],’ into the ‘Replace’ box.

So, is there a way of doing this?

  • 2
    Please [edit] your question and add (a lot) more context. What regular expression flavor are you using? In what context, what tool? What is the relevant regular expression? How a tool refers to matches is not a part of the regular expression language, but of how each tool decides to do it, so we need to know what context you are working in. Sed? Grep? A programming language like Python or Perl? The shell? A text editor? – terdon Feb 21 '24 at 17:47
  • The rather meager documentation suggests you can use either Perl-like $n or sed-like \n to refer to the nth capture group. It's not clear whether there is a whole-match replacement like sed's &. – steeldriver Feb 21 '24 at 21:18
  • Thanks steeldriver, I experimented with \n and $n to no avail. I'll find a different way of solving the problem – Bfuddled2Day Feb 22 '24 at 12:53
  • It may not have been obvious from the documentation, but (with the exception of & that I mentioned) you'd need to capture the pattern in order to use the backreference i.e. (\d\d.\d\d) rather than plain \d\d.\d\d - and the n in that context would be 1 – steeldriver Feb 22 '24 at 13:04
  • ... OK so I just installed the classic snap (Build 4169) on 22.04 and both \1 and $1 work for me – steeldriver Feb 22 '24 at 13:11
  • Please [edit] your question and add: i) an example input file and ii) the output you want from that example. Don't describe your data, show us. Just paste it into your question and use the {} button to format as code. That way, we can test our solutions and ensure that they work as you want. Note that the best way will probably be using an external tool and not Sublime itself. – terdon Feb 22 '24 at 14:14

1 Answers1

1

The rather meager unofficial documentation suggests you can use either Perl-like $n or sed-like \n to refer to the nth capture group:

Sublime Text uses Perl Regular Expression Syntax from the Boost library.

See also

  • Boost library documentation for regular expressions
    • Documentation on regular expressions.
  • Boost library documentation for format strings
    • Documentation on format strings. Note that Sublime Text additionally interprets \n as $n.

So for example (tested on sublime-text build 4169 installed on Ubuntu 22.04 as a classic snap):

sublime text regex capture replacement

Although I can't find it in the documentation, the perl-like $& also works as a replacement placeholder for the entire match, allowing you to omit the explicit capture group parentheses:

sublime text regex entire replacement

steeldriver
  • 136,215
  • 21
  • 243
  • 336