Read more

RubyMine: Find and Replace with Regex (Capture Groups and Backreferences)

Julian
September 20, 2022Software engineer at makandra GmbH

tl;dr

In RubyMine you can use find and replace with capture groups (.*?) and backreferences $1 (if you have several groups: $[Capture-Group ID]).
Named captures (?<text>.*) are also supported.

Examples

Replace double quotes with single quotes

If you want to replace double quotes with single quotes, replacing every " with a ' is prone to errors. Regular expressions can help you out here.

  1. Open find and replace
  2. Activate the regex mode (click on the .* icon next to the "find" field).
  3. Fill in find field with "(.*?)". Your regular expression captures whatever is contained by two double quotes.
  4. Fill in replace field with '$1'. The $1 references what was captured.
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

RubyMine shows you a preview of the result, so you can check if the find and replace is working like expected.
You may use the and icon buttons next to the replace field to cycle through matches.

Image

Refactor an attribute

In this example, we will be using named captures. If you are new to named captures, we suggest reading the excellent Named Capturing Groups and Backreferences Guide on regular-expressions.info Show archive.org snapshot .

You have an attribute that holds a URL. Now you want to change this attribute to hold a list of URLs, and need to adjust all tests.
Our goal is to convert the url "..." to the urls ["..."] here.

  1. Open find and replace
  2. Activate the regex mode
  3. Fill in find field with e.g. url (?<url>.*?)
    Hint: the "url" at the beginning of the string is just a text fragment; the (?<url>.*?) is our named capture group.
  4. Fill in replace field with urls ["${url}"]
    Hint: "urls" is just a text fragment; ${url} references the named capture, and ["${url}"] means that it's put into quotes and brackets.

Image

Refactor "should" syntax to "expect" syntax

  1. Open find and replace
  2. Activate the regex mode
  3. Fill in find field with (\S*?)\.should\s*?==. Your regular expression captures any non-whitespace character that directly precedes .should, followed by any amount of whitspace characters and ==. For other matchers you need to replace the == accordingly.
  4. Fill in replace field with expect($1).to eq. The $1 references is what was captured. For other matchers you need to replace the eq accordingly.

Image

Bonus: If your coding styles require you to pass method arguments in parantheses, you could use a second capture group

  1. Fill in find field with (\S*?)\.should\s*?==\s*?('.*?').
  2. Fill in replace field with expect($1).to eq($2).

Image

Refactor old ruby hash-key syntax with arrow for symbols

  1. Open find and replace
  2. Activate the regex mode
  3. Fill in find field with :(\w*) =>
  4. Fill in replace fields with $1:
Julian
September 20, 2022Software engineer at makandra GmbH
Posted by Julian to makandra dev (2022-09-20 08:32)