gpt4 book ai didi

regex - 替换 VB.Net 代码中的属性定义

转载 作者:行者123 更新时间:2023-12-01 01:27:49 24 4
gpt4 key购买 nike

在 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 的查找和替换工具允许您执行正则表达式,因此我假设必须有一个表达式可以用于执行此转换。

进行这种转换的正则表达式是什么?

最佳答案

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

正则表达式:

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

替换:
${3} Property ${1} As ${2}

我已经使用 RegexBuddy 针对 .NET regex 变体对此进行了测试。请注意,这可能会或可能不会在 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»

关于regex - 替换 VB.Net 代码中的属性定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6585471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com