Google Colab notebook Find and Replace (Ctrl + H) using reg exp. Do capture group backreferences work on replace?

Viewed 902

In the Google Colab notebook menu: Edit :: Find and Replace (Ctrl + H). Tick the "use regular expressions box".

The regexp queries work as expected on find. However, in the replace box I am unable to enter any backreferences for capture groups.

The usual backreference syntax for capture groups is either \1 or $1 (e.g.for capture group 1) However, this does not work in the replace box. If capture group backreferences are supported on Colab notebooks then please would you explain the syntax and if possible give some examples.

1 Answers

COMPROMISE SOLUTION

I could not use the Edit :: Find and Replace (Ctrl + H) with use regular expressions box enabled to accomplish this. However, if you instead enable vim keybindings (Settings>Editor>Editor key bindings), you can accomplish this locally per cell.

While editing your cell of interest, press the colon key :, then use substitution command s/pattern/replacement/flag. Make sure you use \1 within the pattern and $1 within the replacement. This works for me on Colab to add comma to end of line: :s/(.*)/$1,. If I select multiple lines in vim Visual mode, then I can add a comma to the end of every selected line.

Reference

Related