Domanda

I'm working on a fairly large project (c#) and from place to place I have snippets that looks like this:

((int)foo).ToString() + "," + ((int)bar).ToString() + "," + ((int)zig).ToString() + ...

The comma delimited string has arbitrary length (average range from 1 to 5, but I'm not sure whether there's more). so far I'm able to find all the occurrences I want using:

\({.+}\).ToString\(\)( *\+ *"," *\+ *\({.+}\).ToString\(\))*

now for each of the occurrence I want to replace it with something like this:

{ (int)foo, (int)bar, (int)zig , ...}

replacing each occurrence manually is not feasible, is there a replace regex that does the job?

Thanks

È stato utile?

Soluzione

Yes you can do a find and replace in Visual Studio using regular expressions.

In a few words you need to use curly braces for tagged expressions and reuse them using \1, \2, etc.

Altri suggerimenti

I may be misunderstanding your question, but it sounds like you've already got your answer... Use System.Text.RegularExpressions.Regex.Replace(string, string). If you need fine-grained control, consider wrapping your captures with names: \((?<foo>.+)\) (or use indexes). With those captures you can do the replacements manually if need be by looping through the matches and then iterating over the captures.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top