Posted 15 days ago. Visible to the public. Repeats.

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

tl;dr

In RubyMine you can use find and replace with capture groups (.*?) and backreferences $1.
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.

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 Archive .

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

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Avatar
Julian
Last edit:
5 days ago
by Bruno Sedler
Attachments:
find_and_replace_2.png, find_and_replace_3.png, find_and_replace_4.png, find_and_replace.png
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Julian to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more