Replace property definitions in VB.Net code

In VB 2010, you can use implied properties, such as C #, that transform this

Private _SONo As String Public Property SONo() As String Get Return _SONo End Get Set(ByVal value As String) _SONo = value End Set End Property 

IN

 Public Property SONo() As String 

What I want to do is replace the old style with the new style in multiple files. Since Visual Studio's search and replace tool allows you to execute regular expressions, I assume that there must be an expression for this conversion that I can use.

What will be the regular expression for this conversion?

+4
source share
1 answer

This can be dangerous, as you may have logic in property setters / getters, but if they don't have logic, you can say:

Regular expression:

 Private\s_(\w+)\sAs\s(\w+).*?(^\w+).*?Property.*?End\sProperty 

Replace:

 ${3} Property ${1} As ${2} 

I tested this with RegexBuddy, a .NET variant oriented regex. Note that this may or may not work at the Visual Studio Find / Replace prompt, as this is another option.

UPDATE: VS variant (period cannot match newlines, so we need to add this functionality and also convert: \ w =: a, \ s =: b, {} for tags and *? = @):

 Private:b_{:a+}:bAs:b{:a+}(.|\n)@{:a+}(.|\n)@Property(.|\n)@End:bProperty \3 Property \1 As \2 

Regex does the following:

 Options: dot matches newline; case insensitive; ^ and $ match at line breaks Match the characters "Private" literally «Private» Match a single character that is a "whitespace character" (spaces, tabs, and line breaks) «\s» Match the character "_" literally «_» Match the regular expression below and capture its match into backreference number 1 «(\w+)» Match a single character that is a "word character" (letters, digits, and underscores) «\w+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match a single character that is a "whitespace character" (spaces, tabs, and line breaks) «\s» Match the characters "As" literally «As» Match a single character that is a "whitespace character" (spaces, tabs, and line breaks) «\s» Match the regular expression below and capture its match into backreference number 2 «(\w+)» Match a single character that is a "word character" (letters, digits, and underscores) «\w+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match any single character «.*?» Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» Match the regular expression below and capture its match into backreference number 3 «(\w+)» Match a single character that is a "word character" (letters, digits, and underscores) «\w+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match any single character «.*?» Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» Match the characters "Property" literally «Property» Match any single character «.*?» Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» Match the characters "End" literally «End» Match a single character that is a "whitespace character" (spaces, tabs, and line breaks) «\s» Match the characters "Property" literally «Property» 
+4
source

All Articles