Private Sub Form_Load() Form1.Show s = "<a>12345 </a> <a>23456 </a><a>12366745 </a> <a>23789456 </a><a>2345512345 </a> <a>234562345 </a>"s = Replace(s, "<a>", "|") s = Replace(s, "</a>", "|") s = Split(s, "|") For i = 1 To UBound(s) If s(i) <> "" Then Print s(i) End If Next End Sub
最主要的方法有两种: 1 正则表达式。查找所有符合掩码 "</a>*<a>" 的模式,然后去掉两端尖括号内容就可以了。 2 循环遍历:Dim P1 As Long, P2 As Long Dim strSource As StringstrSource = "<a>12345 </a> <a>23456 </a><a>12366745 </a> <a>23789456 </a><a>2345512345 </a> <a>234562345 </a>"P2 = 1 List1.Clear Do Until P2 = 0 P1 = InStr(P2, strSource, "<a>") + 3 If P1 < 4 Then Exit Do P2 = InStr(P1, strSource, "</a>")
If P2 Then List1.AddItem Mid(strSource, P1, P2 - P1) P2 = P2 + 4 Loop
try: Dim s As String Dim tmp As String Dim n As Long
s = "<a>12345</a> <a>23456</a>" s = Replace(s, Chr(32), vbNullString) Do n = InStr(s, ">") + 1 s = Replace(s, "<", Chr(32), n, 1) tmp = Left(s, InStr(s, Chr(32))) If Trim(tmp) <> "" Then Debug.Print tmp Loop Until n = 1
Private Sub Form_Load()
Form1.Show
s = "<a>12345 </a> <a>23456 </a><a>12366745 </a> <a>23789456 </a><a>2345512345 </a> <a>234562345 </a>"s = Replace(s, "<a>", "|")
s = Replace(s, "</a>", "|")
s = Split(s, "|")
For i = 1 To UBound(s)
If s(i) <> "" Then
Print s(i)
End If
Next
End Sub
给出多些测试数据以及期望得到的结果。
最主要的方法有两种:
1 正则表达式。查找所有符合掩码 "</a>*<a>" 的模式,然后去掉两端尖括号内容就可以了。
2 循环遍历:Dim P1 As Long, P2 As Long
Dim strSource As StringstrSource = "<a>12345 </a> <a>23456 </a><a>12366745 </a> <a>23789456 </a><a>2345512345 </a> <a>234562345 </a>"P2 = 1
List1.Clear
Do Until P2 = 0
P1 = InStr(P2, strSource, "<a>") + 3
If P1 < 4 Then Exit Do
P2 = InStr(P1, strSource, "</a>")
If P2 Then List1.AddItem Mid(strSource, P1, P2 - P1)
P2 = P2 + 4
Loop
Dim tmp As String
Dim n As Long
s = "<a>12345</a> <a>23456</a>"
s = Replace(s, Chr(32), vbNullString)
Do
n = InStr(s, ">") + 1
s = Replace(s, "<", Chr(32), n, 1)
tmp = Left(s, InStr(s, Chr(32)))
If Trim(tmp) <> "" Then Debug.Print tmp
Loop Until n = 1
和7楼的差不多
楼主要的分隔不一定是<a></a>,想了下,好象只要用</分组,再找对应的>号: Dim s As String
Dim tmp
Dim i As Long
s = " <a>12345 </a> <A>23456 </A>结果不应该包含这段<b>abc abc</b>"
tmp = Split(s, "</")
For i = 0 To UBound(tmp) - 1
Debug.Print Mid(tmp(i), InStrRev(tmp(i), ">") + 1)
Next