attn: regex gurus. can this be done with a regular expression or using a different technique?

C

ChrisB

Hello:

A third party component (NUnit) being used by my c# code has undergone a
breaking change and, as a result, thousands of methods calls in my source
code need to be revised in the following way:

// General example of the desired revision
RevisedMethod(stringA, stringB) // prior to change
RevisedMethod(stringB, stringA) // after change

Basically, I need to switch *whatever* appears before the comma with
*whatever* appears after the comma. This change needs to take place in
literally thousands of places, and I was wondering if it is possible to
somehow accomplish the switch using the Visual Studio regex search and
replace feature. If so, what would the expression look like?

If this code revision is not possible with a regular expression, is there
perhaps a different developer tool that might accomplish the task? The
thought of manually completing this change is making my head spin!

Thanks!
Chris
 
L

larrylard

ChrisB said:
Hello:

A third party component (NUnit) being used by my c# code has undergone a
breaking change and, as a result, thousands of methods calls in my source
code need to be revised in the following way:

// General example of the desired revision
RevisedMethod(stringA, stringB) // prior to change
RevisedMethod(stringB, stringA) // after change

Basically, I need to switch *whatever* appears before the comma with
*whatever* appears after the comma. This change needs to take place in
literally thousands of places, and I was wondering if it is possible to
somehow accomplish the switch using the Visual Studio regex search and
replace feature. If so, what would the expression look like?

Try this:

Find:
RevisedMethod\((.*), (.*)\)

Replace:
RevisedMethod(\2, \1)

The ( ) in the Find expression 'tag' the captured texdt within, then
the \1 \2 in the Replace expression refer to the 1st and 2nd such
tagged strings from the Find.
 
F

Frans Bouma [C# MVP]

ChrisB said:
Hello:

A third party component (NUnit) being used by my c# code has
undergone a breaking change and, as a result, thousands of methods
calls in my source code need to be revised in the following way:

// General example of the desired revision
RevisedMethod(stringA, stringB) // prior to change
RevisedMethod(stringB, stringA) // after change

Basically, I need to switch whatever appears before the comma with
whatever appears after the comma. This change needs to take place in
literally thousands of places, and I was wondering if it is possible
to somehow accomplish the switch using the Visual Studio regex search
and replace feature. If so, what would the expression look like?

If this code revision is not possible with a regular expression, is
there perhaps a different developer tool that might accomplish the
task? The thought of manually completing this change is making my
head spin!

Yes I think it is possible to do that. You have to create a regex
which matches 2 elements separated with a comma and place them into
groups (named). Then you use replace codes in the regex to replace
groupA with groupB and vice versa. At least that's where I'd look into.

Be sure you're using proper tools to test the regex you'll use. So
first try the matching in groups, then try the replace using groups.
But perhaps there's already a regex available which swaps two strings
separated with a comma on the various regex sites.

FB

--
 
C

ChrisB

Thanks to Larry and Frans for their help.

Larry, your suggestion worked very well. Just had to make one change.
Apparently, (for some reason!?!?) Microsoft decided to switch the capture
characters from parentheses to brackets when using regular expressions
within Visual Studio's search/replace functionality.

So the final expressions that worked are:
Search: Test\({.*}, {.*}\)
Replace: Test(\2, \1)

Thanks Again!
Chris
 
V

Vicente García

ChrisB said:
Thanks to Larry and Frans for their help.

Larry, your suggestion worked very well. Just had to make one change.
Apparently, (for some reason!?!?) Microsoft decided to switch the capture
characters from parentheses to brackets when using regular expressions
within Visual Studio's search/replace functionality.

So the final expressions that worked are:
Search: Test\({.*}, {.*}\)
Replace: Test(\2, \1)

Thanks Again!
Chris
 
M

Mike Blake-Knox

Microsoft decided to switch the capture
Out of curiosity, would anyone know why VS's regular expressions aren't the
.NET Framework ones?

Thanks

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top