Oscar Vasquez
Oscar Vasquez

Reputation: 465

Visual Studio: Find / Replace using regular expression to replace

I'm using the Find / Replace tool of visual studio to find something using regular expressions and make a replace. I have this in the find: Assert.IsTrue\(([^,;]*)\) *; and the replace Assert.IsTrue($1, "$1");, so what this does is looking for every Assert.IsTrue(); whith anything in the parentheses except for commas , and semicolons ;, and then add whatever was on the parentheses inside quotes and after a comma ,. So, if I have Assert.IsTrue(wtv) it will be replaced with Assert.IsTrue(wtv,"wtv").

The problem is when the wtv has quotes or break lines, so if I have

Assert.IsTrue("wtv" == "wtv") it will be replaced to

Assert.IsTrue("wtv" == "wtv", ""wtv" == "wtv"") and

Assert.IsTrue(wtv ||
wtv2)

will be replaced to

Assert.IsTrue(wtv ||
wtv2, "wtv ||
wtv2")

. What I want to do is eliminate in the replacement the new line \r and the quotes, so the results after the replacement are

Assert.IsTrue("wtv" == "wtv", "wtv == wtv") and

Assert.IsTrue(wtv ||
    wtv2, "wtv ||wtv2")  

Upvotes: 1

Views: 86

Answers (1)

Oscar Vasquez
Oscar Vasquez

Reputation: 465

First I'll clarify that this doesn't really solve the problem, is just a nasty work around, not a real solution. I post it just in case someone needs a work around as I do (I doubt it but well). Still, as this is not the real answer I'll not mark it as so (unless someone explains me that it's not possible a real answer), and new answers are always welcomed.

What I did was in the part that need regex add several groups that ([^,;"\r\n]*) first look for anything that it's not a comma, semicolon, quote or new-line, then look for (["\r\n]*) ne-line or semicolon, and then repeat this pattern several times.

So, what this will do as it's using * it will look if it happens 0 or more times, and is repeated several times in case that there is more than one comma or more than one new-line (note that if there are none, that's not a problem since I'm using *). And, the replace would look like

Assert.IsTrue($1$2$3..., "$1$3$5...");

where in the first argument I put all the numbers, and in quotes I put only the odd numbers since the even are either non existent or quote / new-line.

I used 31 of these, so if there are more than 15 groups of commas / new-line, it will not be found and replaced The find

Assert.IsTrue\(([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)(["\r\n]*)([^,;"\r\n]*)\) *;

The replace

Assert.IsTrue($1$2$3$4$5$6$7$8$9$10$11$12$13$14$15$16$17$18$19$20$21$22$23$24$25$26$27$28$29$30$31, "$1$3$5$7$9$11$13$15$17$19$21$23$25$27$29$31");

This works for the examples I provided and for any example with less than 15 groups of commas / new-lines, if I can come up with something better (since this a really crappy solution), I'll add it here.

Upvotes: 1

Related Questions