1.根据:
VB.Net中的正则表达式的类是System.Text.RegularExpressions
2.接下来,就是参考资料,去如何使用了。
选择RegularExpressions后,按F1:
然后就可以找到官网的解释:
System.Text.RegularExpressions Namespace
3。期间,折腾了多行字符串,详见:
4.期间,也需要搞懂,如何在正则的pattern中包含双引号:
【整理】VB.Net中的(正则表达式的Pattern)字符串中,如何包含双引号
5.最后,终于写完成了一个完整的例子,如何使用VB.Net中的正则的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | Imports System.Text.RegularExpressions Imports System.Xml Imports System.Xml.Linq Public Class Form1 Private Sub Form1_Load(sender As System. Object , e As System.EventArgs) Handles MyBase .Load Dim s As String = <a>Hello World</a>.Value Dim inputHtml As String inputHtml = <a><![CDATA[<div class= "d_list_txt" id= "d_list" > <ul> <li><span class= "c_tit" >... <li><span class= "c_tit" >... <li><span class= "c_tit" >... <li><span class= "c_tit" >... <li><span class= "c_tit" >... </ul> <div class= "pagebox" >]]></a>.Value Dim contentP As String contentP = "<div class=" "d_list_txt" " id=" "d_list" ">(?<youWant>.+)<div class=" "pagebox" ">" Dim contentRx As Regex contentRx = New Regex(contentP, RegexOptions.Singleline) Dim foundImg As Match foundImg = contentRx.Match(inputHtml) If (foundImg.Success) Then Dim youWant As String youWant = foundImg.Groups( "youWant" ).Value MessageBox.Show(youWant) End If End Sub End Class |
【总结】
VB.Net中使用正则,和C#中,基本类似,但是还是语法上,有些细微的差别。
对于不熟悉VB.Net的,还是需要一番折腾,才能写出正确的代码的。
注意:
此处,由于用了
Imports System.Xml
Imports System.Xml.Linq
所以,是需要VB.Net是4.0版本的。
详见:
转载请注明:在路上 » 【记录】VB.Net中的正则表达式