5

Our code base has a lot of StringBuilder.AppendFormats that have strings that end with newline characters. I've made an extension method called AppendLineFormat and would like to use Resharper's Custom Patterns feature to identify and fix those old calls to use the new extension method (and to suggest this new extension method to others in the future).

StringBuilder sb = new StringBuilder();
int i = 1;
sb.AppendFormat("i is {0}\r\n", i);

into

sb.AppendLineFormat("i is {0}", i);

Is there a way to use regex (with replacement) to identify strings that match? I don't want to turn every AppendFormat into an AppendLineFormat - only the ones with strings that end in \r\n. Here's what I've got so far.

Search Pattern:

$sb$.AppendFormat($newLineString$,$args$)

Replace Pattern:

$sb$.AppendLineFormat($newLineString$,$args$)

Where

  • $sb$ is an expression of type System.Text.StringBuilder
  • $newLineString$ is an identifier matching "(.*)\\r\\n" (totally wrong, I know)
  • $args$ is any number of arguments
marchica
  • 2,326
  • 23
  • 21

1 Answers1

2

Unfortunately as for now it's not supported.

There is a bunch of issues, you can vote for them:

As a workaround you can use Visual Studio Find and Replace feature.

Search pattern:

\.AppendFormat\(\"(.+?)(?>\\r\\n\")

Replace pattern:

.AppendLineFormat("$1"

enter image description here

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39