Заменить определения свойств в коде VB.Net - PullRequest
4 голосов
/ 05 июля 2011

В VB 2010 вы можете использовать подразумеваемые свойства, такие как C #, что превращает это

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

в

Public Property SONo() As String

Что я хочу сделать, это заменить старый стиль новымстиль в нескольких файлах.Поскольку инструмент поиска и замены в Visual Studio позволяет выполнять регулярные выражения, я предполагаю, что должно быть выражение, которое я мог бы использовать для этого преобразования.

Каким будет регулярное выражение для этого преобразования?

1 Ответ

4 голосов
/ 05 июля 2011

Это может быть опасно, поскольку у вас может быть логика в установщиках / получателях свойств, но если у них нет логики, вы можете сказать:

Регулярное выражение:

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

Заменить:

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

Я проверил это с помощью RegexBuddy, нацеленного на вариант регулярного выражения .NET.Обратите внимание, что это может работать, а может и не работать, в приглашении Найти / Заменить Visual Studio, поскольку это еще один вариант.

ОБНОВЛЕНИЕ: вариант VS (точка не может соответствовать символам новой строки, поэтому нам нужночтобы добавить эту функциональность, также преобразуются: \ w =: a, \ s =: b, {} для тегов и *? = @):

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

\3 Property \1 As \2

Regex выполняет следующие действия:

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»
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...