Rizwan Ahmed
Rizwan Ahmed

Reputation: 334

Find and replace word with gap/wildcard in Android Studio

Is there any way to find a specific code with one or more gaps? For example, I want to replace

.setImageResource(R.drawable.*)

with

.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.*))

Here * means the name will remain as was before.

Upvotes: 3

Views: 872

Answers (1)

iknow
iknow

Reputation: 9902

Yes it is possible. Click Ctrl+Shift+R. You will see something like this:

enter image description here

Select Regex and then in first field paste:

.setImageResource\(R.drawable.(.*)\)

and in the second:

.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.$1))

This replacement based on Regex. In the first statement, You set the group after R.drawable in the () brackets. This group can be used in the second statement by using $1. 1 is the number of the group. 0 is the full regex and the next groups start integrating from 1.

Result:

.setImageResource(R.drawable.something1)
.setImageResource(R.drawable.something2)

was changed to

.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.something1))
.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.something2))

Here You can read more about Find and replace with Regex in IntelliJ IDEA

Upvotes: 5

Related Questions