0

I have lots of code like below:

PlusEnvironment.EnumToBool(Row["block_friends"].ToString())

I need to convert them to something like this.

Row["block_friends"].ToString() == "1"

The value that gets passed to EnumToBool is always unique, meaning there is no guarantee that itll be passed by a row, it could be passed by a variable, or even a method that returns a string.

I've tried doing this with regex, but its sort of sketchy and doesn't work 100%.

PlusEnvironment\.EnumToBool\((.*)\)

I need to do this in Visual Studio's find and replace. I'm using VS 17.

cardnc
  • 93
  • 4
  • 1
    Probably more trouble than it's worth but you can maybe use a RegEx to match balanced parentheses: https://stackoverflow.com/a/7899205/224370 Unless you have thousands it would be easier to just to fix the first part with a normal replace and then manually fix the syntax errors for the remainder replacing the ) with =="1" until all the red has gone. – Ian Mercer Mar 24 '18 at 04:51

2 Answers2

0

If you had a few places where PlusEnvironment.EnumToBool() was called, I would have done the same thing that @IanMercer suggested: just replace PlusEnvironment.EnumToBool( with empty string and the fix all the syntax errors.

@IanMercer has also given you a link to super cool, advanced regex usage that will help you.

But if you are skeptical about using such a complex regex on hundreds of files, here is what I would have done:

Define my own PlusEnvironment class with EnumToBool functionality in my own namespace. And then just replace the using Plus; line with using <my own namespace>; in those hundreds of files. That way my changes will be limited to only the using... line, 1 line per file, and it will be simple find and replace, no regex needed.

(Note: I'm assuming that you don't want to use PlusEnvironment, or the complete library and hence you want to do this type of replacement.)

Vivek Athalye
  • 2,974
  • 2
  • 23
  • 32
-1

in Find and Replace Window: Find: PlusEnvironment\.EnumToBool\((.*))

Replace: $1 == "1"

Make sure "Use Regular Expressions" is selected

Matt.G
  • 3,586
  • 2
  • 10
  • 23
  • 1
    I think the Op knows how to use VS, the question is how to match balanced parentheses when there may be another set inside. – Ian Mercer Mar 24 '18 at 04:53