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

4

在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
这可能会很危险,因为你可能在属性的setter/getter中有逻辑,但如果它们没有逻辑,你可以这样说: 正则表达式:
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

正则表达式的作用如下:
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»

将其转换为查找/替换格式后,它看起来像这样:Private _{:w*}:bAs:b{:w*}(.|\n){:w}:bProperty(.|\n)*End Property,我认为这是正确的,但当我尝试进行查找时,它会锁定VS,因此我无法测试它。VS的查找和替换可能无法处理这个。 - Kratz
@Kratz,我已经更新了它以解决它存在的问题。我还更新了它以包括VS变体。 - Matt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接