使用 vb.net 和 RegEx 在嵌套字符串中查找字符串 [英] Using vb.net and RegEx to find string inside nested string

查看:51
本文介绍了使用 vb.net 和 RegEx 在嵌套字符串中查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VB.NET,有没有办法在 1 步中完成这个 RegEx 调用...而不是 2-3 步?

Using VB.NET, Is there a way to do this RegEx call in 1 step... instead of 2-3?

我正在尝试找到bingo"这个词,或者 START 和 END 之间的任何词,但是然后也在里面 FISH 和 CAKES 字样.我的最终结果应该只是宾果游戏".

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

推荐答案

对 VB.NET 不确定,但您可以使用以下 RegExp 捕捉内部宾果游戏":

Not sure about VB.NET, but you can catch the inner "bingo" using the following RegExp:

START.*FISH.*(bingo).*CAKES.*END

Bingo"将成为此表达式的第一个(也是唯一一个)匹配项.

"Bingo" will be then the first (and the only) match of this expression.

这篇关于使用 vb.net 和 RegEx 在嵌套字符串中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆