1

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")  
3
  • I would write a console application which would do the same with regular expressions instead trying to use the limited VS Regex. Commented Jan 24, 2016 at 22:27
  • @OmegaMan at the end this is what will be done, I was exploring several possibilities and was curious for the question. Thanks Commented Jan 27, 2016 at 23:38
  • I love regex, don't get me wrong; just that multiple line crossings are difficult enough to handle in regex (usually used by a negative look ahead), but near impossible in VS interpretation of the pattern. :-) It doesn't hurt to ask and to that your post is excellent. Commented Jan 27, 2016 at 23:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this will be much slower, the more capturing groups you add, the slower it will be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.