Reputation: 3987
Not sure if this is possible, but I'm trying to do a dynamic Find/Replace in IntelliJ IDEA...
In my Java project, there are several methods that contain string concatenation like this:
return "[foo=" + foo + ", bar=" + bar + ", this=" + this + ", that=" + that + "]";
I wrote the following RegEx to find all of these occurrences:
return (\"[^\"]+\")+((\s\+\s)+([\pL\pN\r\s]*)+(\s\+\s)+(\"[^\"]+\"))*;
Now I want to replace these concatenations with equivalent String.format()
returns, like this:
return String.format("[foo=%s, bar=%s, this=%s, that=%s]", foo, bar, this, that);
My replacement RegEx looks like this so far:
return String.format\($1$6,$4\);
...but a couple of things are happening here, and a couple of tricky requirements.
$4
is empty.$6
only returns the last occurrence of (\"[^\"]+\")
.$4
.$1
and $6
and append a "%s" after any "=" signs.Is this possible? Any idea how to do it, allowing for concatenation of any number of strings?
Thanks in advance!
Upvotes: 0
Views: 201
Reputation: 4764
You don't need Regex find-and-replace. If you put your cursor in the middle your your String concatenation, a yellow light bulb should appear. If you click the light bulb (or better yet, alt-Enter), it should give you at least three different refactorings of that line of code.
See the Intentions documentation here.
Upvotes: 2