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.
- Open find and replace
- Activate the regex mode (click on the
.*
icon next to the "find" field). - Fill in find field with
"(.*?)"
. Your regular expression captures whatever is contained by two double quotes. - 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.
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.
- Open find and replace
- Activate the regex mode
- 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. - 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.
Refactor "should" syntax to "expect" syntax
- Open find and replace
- Activate the regex mode
- 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. - Fill in replace field with
expect($1).to eq
. The$1
references is what was captured. For other matchers you need to replace theeq
accordingly.
Bonus: If your coding styles require you to pass method arguments in parantheses, you could use a second capture group
- Fill in find field with
(\S*?)\.should\s*?==\s*?('.*?')
. - Fill in replace field with
expect($1).to eq($2)
.
Refactor old ruby hash-key syntax with arrow for symbols
- Open find and replace
- Activate the regex mode
- Fill in find field with
:(\w*) =>
- Fill in replace fields with
$1: