RegEx 匹配 VB.NET 选择大小写,不带其他大小写
我正在寻找一个正则表达式,它将找到其中没有 Case Else 的 Select Case 语句。
这是我到目前为止所想到的,
(?sm)^\s*Select Case.*(?<!^\s*Case Else.*)End Select
除了可能有嵌套语句的情况之外,这个效果非常好。
在我尝试使用平衡组时,我想出了以下
Select Case(?>Select Case(?<DEPTH>)|End Select(?<-DEPTH>)|.?)*?(?(DEPTH)(?!))End Select
正确找到选择案例/结束选择的平衡组的方法,但我很难让它与 (?
下面是一些示例数据:
Select Case
Case :
Select Case
Case : Something
End Select
Case Else : SomethingElse
End Select
在这种情况下,它应该只匹配内部 Select Case,因为外部有它的 Case Else
Select Case
Case :
Select Case
Case : Something
Case Else : SomethingElse
End Select
End Select
应该匹配整个块,因为内部有 Else 但外部没有。
Select Case
Case :
Select Case
Case : Something
Case Else : SomethingElse
End Select
Case Else : SomethingElseOutter
End Select
不应匹配,因为内部和外部选择都有 Case Else
I'm looking for a RegEx that will find Select Case Statements that have no Case Else in them.
Here's what I came up with so far
(?sm)^\s*Select Case.*(?<!^\s*Case Else.*)End Select
This one works perfectly except for cases that may have nested statements.
in my attempt to use balance groups, I came up with the following
Select Case(?>Select Case(?<DEPTH>)|End Select(?<-DEPTH>)|.?)*?(?(DEPTH)(?!))End Select
Which correctly finds balanced groups of Select Case/End Selects but I'm having a hard time getting it to work with the (?
Heres some sample data:
Select Case
Case :
Select Case
Case : Something
End Select
Case Else : SomethingElse
End Select
In this case it should match just the inner Select Case because the Outter has it's Case Else
Select Case
Case :
Select Case
Case : Something
Case Else : SomethingElse
End Select
End Select
Should match then entire block because the inner has the Else but the outter doesnt.
Select Case
Case :
Select Case
Case : Something
Case Else : SomethingElse
End Select
Case Else : SomethingElseOutter
End Select
Should not match because both inner and outter selects have a Case Else
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正则表达式
一行:
在 regexstorm.net 上测试
描述
正则表达式匹配
Select Case
,然后尝试消耗尽可能少的行尽可能直到找到End Select
。对于每一行:Select Case
,它会在(?Select[ ]Case)
|
false)
是一个 IF 子句:nested
的捕获(即在嵌套语句内),它可以在(?<-nested>End[ ]Select)?
时减去捕获匹配(可选组)。(?!Case[ ]Else)
。这是平衡组背后的逻辑。如果它与嵌套的
Select Case
匹配,则会创建一个新的捕获,如果它与End Select
匹配,则会减去最后一个捕获。因此,仅在外部组中不会存储捕获。我们在模式的末尾使用
(?(nested)(?!)|[ \t]*End[ ]Select)
。如果有捕获,它会转到(?!)
(永远无法匹配),失败并回溯以继续消耗更多行。但如果没有捕获,它可以匹配[ \t]*End[ ]Select
(或回溯并消耗更多行)。就是这样。
但请注意,如果有两个没有 Case Else 的 Select 语句嵌套在另一个语句中,则只有外部语句会匹配。如果您有兴趣匹配两者,请
使用
Match.Groups(1)
来匹配文本。Regex
One-liner:
Test on regexstorm.net
Description
The regex matches
Select Case
and then it tries to consume as few lines as it can until it finds anEnd Select
. For each line:Select Case
, it creates a Capture in(?<nested>Select[ ]Case)
(?(nested)
true|
false)
is an IF clause that:nested
(i.e. inside a nested statement), it could substract a capture when(?<-nested>End[ ]Select)?
matches (optional group).(?!Case[ ]Else)
.This is the logic behind balancing groups. If it matches a nested
Select Case
, it creates a new capture, and if it matches anEnd Select
it substracts the last capture. Therefore, only in the outer group there will be no captures stored.We use that at the end of the pattern with
(?(nested)(?!)|[ \t]*End[ ]Select)
. If there's a capture it goes to(?!)
(which can never match), fails and backtracks to keep consuming more lines. But if there are no captures, it can match[ \t]*End[ ]Select
(or backtrack and consume more lines as well).That's it.
Notice though that if there are two Select staments without a Case Else, nested one in another, only the outer statement will be matched. if you're interested in matching both, use
and use
Match.Groups(1)
to get the text matched.