在VB.Net代码中替换属性定义

时间:2011-07-05 15:51:24

标签: regex vb.net visual-studio-2010

在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 个答案:

答案 0 :(得分:4)

这可能很危险,因为你可能在属性设置器/ getter中有逻辑,但如果它们没有逻辑,你可以说:

正则表达式:

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

替换:

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

我用RegexBuddy针对.NET正则表达式变体进行了测试。请注意,这可能在Visual Studio查找/替换提示中有效,也可能无效,因为这是另一种变体。

更新: VS的变体(Dot无法匹配换行符,因此我们需要添加该功能,并对标签进行转换:\ w =:a,\ s =:b,{},以及*?= @):

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

\3 Property \1 As \2

正则表达式执行以下操作:

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»