获取两个字符串之间的文本 Regex VB.Net [英] Get text between two strings Regex VB.Net

查看:44
本文介绍了获取两个字符串之间的文本 Regex VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对正则表达式有严重的问题.我需要获取 2 个字符串之间的所有文本,在这种情况下,字符串是 <span class="user user-role-registered-member"></span>.

I really have serious problems with regex. I need to get all text between 2 strings, in this case that strings are <span class="user user-role-registered-member"> and </span>.

我在谷歌上搜索了很多问题(其中一些在 StackOverFlow 上),并观看了 YouTube 教程,但仍然无法得到.

I googled pretty much questions (some of them are on StackOverFlow), and watched YouTube tutorials, still can't get it.

这是我认为可行的代码,但我不知道为什么不可行.

This is the code that i think would work, but i don't know why it doesn't.

Dim mystring As String = "<br>Terms of Service<br></br>Developers<br>"

Dim pattern1 As String = "(?<=<br>)(.*?)(?=<br>)"
Dim pattern2 As String = "(?<=</br>)(.*)(?=<br>)"

Dim m1 As MatchCollection = Regex.Matches(mystring, pattern1)
Dim m2 As MatchCollection = Regex.Matches(mystring, pattern2)
MsgBox(m1(0).ToString)
MsgBox(m2(0).ToString)

好的,所以这段代码工作得很好......使用<br>.我试图用 span 更改 pattern1 和 pattern2 的 <br> 但它不起作用.我知道我在这里犯了一个错误,但我不知道在哪里/如何.

Ok, so this code works pretty well....with <br>. I tried to change pattern1 and pattern2's <br> with span but it doesn't work. I know that i am making a mistake here, but i don't know where/how.

任何答案都会非常有帮助.

Any answer will be really helpful.

推荐答案

这可以轻松而精美地完成工作.当跨度内没有文本时,它不会返回匹配项,因此您无需担心空匹配项.然而,它会返回只有空格的组.

This does the job easily and beautifully. It won't return a match when there is no text inside the span, so you do not need to worry about empty matches. It will however return groups with only whitespace in them.

<span class=""user user-role-registered-member"">(.+)</span>

测试一下 此处.

这篇关于获取两个字符串之间的文本 Regex VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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