apukineimakiapudjuh
apukineimakiapudjuh

Reputation: 93

REGEX Visual Studio 2013 find and change

I want to change naming of my class names and methods in tests. There are hundreds of them, they are very long and hard to read. Underscores will help.

I created REGEX:

(public class [A-Z][a-z]*(_[A-Z][a-z]*)*)(?<x>[A-Z])

Replace pattern: $1_${x}

that gives my example: From: public class VeryLongClassNameTestScenarioTwentySeven

To: public class Very_LongClassNameTestScenarioTwentySeven

Next use: public class Very_Long_ClassNameTestScenarioTwentySeven

Next use: public class Very_Long_Class_NameTestScenarioTwentySeven

After using many times all names should be changed. Can you help me find regular expression that will do it at once?

I suppose regular expressions in VS do not support converting letter from upper, to lower case?

Thanks

Upvotes: 1

Views: 180

Answers (1)

pepoluan
pepoluan

Reputation: 6780

I don't have access to my VS workstation right now, but I think the following should work:

Find: (?<=public class .*?)([a-z])([A-Z])

Replace with: $1_$2

(I tested on http://regexstorm.net/tester )


Edit: Now that I'm sitting in front of my VS workstation, here's the result of my testing:

It "kinds of" work... strangely, with the following code:

public class ThisIsLongClassName
{ }

public class AnotherLongClassName
{ }

public class WhoaThisNameIsEvenLonger
{ }

Now, testing with Shift+Ctrl+F:

  • If I do "Find Next", it properly jumps between the locations where substitutions should take place. So the regex is correct.
  • However, when I clicked "Replace", it replaces the line below the line being highlighted. E.g., the highlight is on sI of the first line, the change happens on rL of the second line
  • Lastly, clicking "Replace All" doesn't do anything

I think VS2013 is b0rked. At least mine is.


Edit 2: So, I recommend you create a small program and let it 'have a go' through your files. Will be much simpler regex, since all you have to do is ensure that the .TrimStart()-ed line starts with 'public class', then do a simple regex replace of ([a-z])([A-Z]) with $1_$2 on that line.

Upvotes: 1

Related Questions