使用 vb.net 和 RegEx 在嵌套字符串中查找字符串
使用 VB.NET,有没有办法在 1 步中执行此 RegEx 调用...而不是 2-3 步?
我正在尝试查找单词“bingo”,或者 START 和 END 单词之间的任何内容,但是 然后里面还有FISH和CAKES字样。我的最终结果应该只是“宾果”。
Dim s1 As String = "START (random string) FISH bingo CAKES (random string) END"
Dim m As Match
m = RegEx.Match(s1, "START\w*END")
If (m.Success) Then
Dim s2 As String = m.Groups(0).ToString()
m = RegEx.Match(s2, "FISH\w*CAKES")
if(m.Success) then
s2 = m.Groups(0).ToString()
m = RegEx.Match(s2, "bingo")
s2 = m.Group(0).ToString()
End If
End If
Using VB.NET, Is there a way to do this RegEx call in 1 step... instead of 2-3?
I'm trying to find the word "bingo", or whatever is between the START and END words, but
then also inside the inner FISH and CAKES words. My final results should be just "bingo".
Dim s1 As String = "START (random string) FISH bingo CAKES (random string) END"
Dim m As Match
m = RegEx.Match(s1, "START\w*END")
If (m.Success) Then
Dim s2 As String = m.Groups(0).ToString()
m = RegEx.Match(s2, "FISH\w*CAKES")
if(m.Success) then
s2 = m.Groups(0).ToString()
m = RegEx.Match(s2, "bingo")
s2 = m.Group(0).ToString()
End If
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定 VB.NET,但您可以使用以下正则表达式捕获内部“bingo”:
“Bingo”将成为该表达式的第一个(也是唯一一个)匹配项。
Not sure about VB.NET, but you can catch the inner "bingo" using the following RegExp:
"Bingo" will be then the first (and the only) match of this expression.
您可以使用lookahead和lookbehind:
但我认为使用捕获组更简单,如@Alaudo建议的:
You can use lookahead and lookbehind:
But I think it's simpler to use a capturing group as @Alaudo suggested: